I am looking for some advice on how to pass my database values into a page.
1 have created 2 pages
1 - Sales Data Display page
2 - Salesdata.php that holds the connection to my database.
I am an newbiew to php, so i may have made some mistakes.
I found some examples on the internet to help me so far
salesdata.php
<?php
//$id = $_GET['id'];
$id=['id'];
// If id is number
if( (int)$id == $id && (int)$id > 0 ) {
// Database connection and select database
$link = mysqli_connect('localhost','root','','Customers');
$sql='SELECT * FROM sales WHERE id=' .$id;
$result = mysqli_query($sql,$link);
$row = mysql_fetch_array($result);
print_r( $row );
}
else {
echo "Record NOT FOUND";
}
?>
SalesDataDisplay.html
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
</body>
<a href="SalesData.php?id=3">Sales Data for ID : 3</a>
<a href="SalesData.php?id=22">Sales Data for ID : 22</a>
<a href="SalesData.php?id=27">Sales Data for ID : 27</a>
</html>
While the php says no record found.
on my html page all i get is the php exchoed out, if that makes sense
I may have mixed up the 2 pages as well - so im not quite sure what to do and how
ive been fiddling about with it all day, uncommenting commenting etc.
I uncommented as suggested the error now is : Undefined index: id
I believe i will need a query string which i constructed on my display page
<a href="SalesData.php?id=3">Sales Data for ID : 3</a>
<a href="SalesData.php?id=22">Sales Data for ID : 22</a>
<a href="SalesData.php?id=27">Sales Data for ID : 27</a>
When I click on the link it should output the database record for the ID - is that not correct
Can you show the code you have now? Typos aside, thereâs no reason that it should not work if youâre calling it from those links. What happens if you add
Youâre a sitting duck for SQL Injection attacks with your code! Have a read through the PHP manual about the use of prepared statements. A golden rule is to NEVER EVER trust any user no matter how well you know them
Is there any weirdness attached to the fact that your links are outside the html <body></body> tags? I canât imagine they would be, and of course you would notice the link being wrong when you hover over it.
Also when i click on one of the links on SalesDataDisplay.html
0 ) { $link = mysqli_connect('localhost','root','','Customers'); // Connect to Database $sql='SELECT * FROM sales WHERE id=' .$id; $result = mysqli_query($sql,$link); $row = mysql_fetch_array($result); // Show record with HTML here print_r( $row ); } else { echo "Record NOT FOUND"; } ?>
To be fair the input is being casted to an int and being checked against 0. I agree that prepared statements and variable binding should be used but the approach being used is sufficient for expected integers.
Is the page being accessed via a GET request and does it include an id?
That output suggests that something immediately before is causing the server to come out of PHP and just start displaying the rest of the script. Are you sure that your XAMPP is configured correctly? And when you run the html page, do you run is as http://localhost/salesdatadisplay.html so that itâs run from the server, and not just by double-clicking it in Windows Explorer (or whatever file explorer you are using)?
Is that the script exactly as it appears on your machine, without any other bits missing or altered, for example a spurious typo that put a ? immediately before the > symbol? I only ask because it doesnât make a lot of sense, I canât see anything in the posted code that would cause it to exit PHP âmodeâ, and you said earlier that the mismatch of html orders was down to bad copy-and-paste. Could there be something we canât see?
Hello ds,
that is a very good observation.
Yes i was using notepad++.
I also have expression web.
My file is located here C:\Users\Dan\Documents\My Web Sites\WebDev\salesdatadisplay.html
I was using the application to launch the file.
My xampp is on port 81, but localhost for expression web is on another port.
Now i dont know if any of this is the culprit.
I need to access the file from xampp localhost i surmise.
let me test
so i have moved my 2 files to htdocs in xampp,
I think im nearly there.
here is the code being used
SalesData.php
<?php
//var_dump($_GET);
$id = $_GET['id'];
if( (int)$id == $id && (int)$id > 0 ) { // If ID is number
$link = mysqli_connect('localhost','root','','Customers'); // Connect to Database
if (!$link) {
die('Could not connect: ' . mysqli_connect_error());
}
$sql='SELECT * FROM sales WHERE id=' .$id;
$result = mysqli_query($sql,$link);
$row = mysql_fetch_array($result);
print_r($row); // Show record with HTML here
}
else {
echo "Record NOT FOUND";
}
?>
HTML File
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<a href="SalesData.php?id=3">Sales Data for ID : 3 </a>
<p></p>
<a href="SalesData.php?id=5">Sales Data for ID : 5 </a>
<p></p>
</body>
</html>
Error Message
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\SalesData.php on line 18
Fatal error: Uncaught Error: Call to undefined function mysql_fetch_array() in C:\xampp\htdocs\SalesData.php:20 Stack trace: #0 {main} thrown in C:\xampp\htdocs\SalesData.php on line 20
Exactly as the error message tells you. Your code supplies a string (the query string) as the first parameter, when the function wants the mysqli link object first.
$result = mysqli_query($sql,$link);
The other error is because you have mis-spelled the function name. And in any case, you need to check if the query worked before using the results.
<?php
$id = $_GET['id'];
if( (int)$id == $id && (int)$id > 0 ) { // If ID is number
$link = mysqli_connect('localhost','root','','Customers'); // Connect to Database
if (!$link) {
die('Could not connect: ' . mysqli_connect_error());
}
$sql='SELECT * FROM sales WHERE id=' .$id;
$result = mysqli_query($link,$sql);
$row = mysqli_fetch_array($result);
echo $row['id'];
echo $row['name'];
}
else {
echo "Record NOT FOUND";
}
?>
I swear i fine combed it a zillion times and still missed the function mispelling as well as upside down , the database connection - oh well newbies got to learn.
Then i mixed and matched some of the old and new well it was all down hill from there
Reading and decoding the error messages are not very intuitive its hard to know exactly whats missing a semicolon a whitespace oh the joys of php.
Thanks folks for perservering with me
I think thats enough php for me for a week at least