PHP - Query Database - Pass Values in URL - That Displays Results in Another Page - Click on Link


#1

Hello all,

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

please do advise and thank you for your help


#2

//$id = $_GET['id'];

$id=['id'];

It's these two lines. You don't have a value in the id. Uncomment the first line (remove the //) and comment out the second (add the two //)

You'll also need to pass a value (either through a form or through the querystring)


#3

Hello Dave,

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

thanks for the tips


#4

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

var_dump($_GET);

at the start of your PHP code?


#5

Hello DS,
The code is exactly the same above.
I simply uncommented the line that Dave suggested

$id = $_GET['id'];

It continuously gives me an error on that specific line.

I've Googled the reasons why but I still can't find anything that works.
I have a php page with connection to my database.

And one HTML page to display my data with the links setup as shown above

My database is working
I am using xampp
I will try your suggestion thank you

It seemed like a simple task to pass the parameter in the URL from my database
But sadly nothing is working


#6

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


#7

and commented out the next line, as Dave also said?


#8

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.


#9

I wondered how long it would take to get this. I figured better to learn to walk before attempting to run....


#10

Thank you for your help, folks,
as you can see im a hopeless newbie with some good errors spotted.

var_dump($_GET);
$id = $_GET['id'];
Error
Notice: Undefined index: id in C:\Users\Dan\Documents\My Web Sites\WebDev\SalesData.php on line 5

it keeps complaining about the $id = $_GET['id'];


#11

:joy: oh dear - I must see the irony there, of course major faux pas - blame it on the bad copy paste


#12

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"; } ?>

this above is displayed on SalesData.php


#13

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?


#14

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?


#15

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


#16

Hello folks,

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

any advice welcome
thank you


#17

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.

http://php.net/manual/en/mysqli.query.php


#18

Hello DS &folks,

thanks for the excellent troubleshooting and insights.

So the errors i made

I was using Expression Web and notepad++ and launching the files from there, that is a no no!

I moved the files to XAMPP > htdocs folder

Accessed the urls as suggested to

http://localhost:81/SalesDataDisplay.html

which when clicked then gave etc

http://localhost:81/SalesData.php?id=5

So the final code that worked is as below

<?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 :stuck_out_tongue_closed_eyes:

Have an awesome week !
:slight_smile:


#19

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.