Href link with ID

Hi, i am a newbie in php programming. I tried to solve my problem many hours, but without any results.
I want to create a href link with unique ID ( ID from db mysql ). After clicking this link you go to the ID info page …
index.php code:

<?php

$servername = "localhost";
$username = "root";
$password = "";

$conn = mysql_connect($servername, $username, $password);
$db = mysql_select_db("doggy");


if(!$conn || !$db)
{
exit(mysql_error());	
}

$result = mysql_query(" SELECT * FROM dog ");
mysql_close();

while($row = mysql_fetch_array($result))
{

echo "<figure class=\"bw\"><a href=\"info.php?id='".$row['id']."'\"><img style=\"margin-right:10px\" width=\"300\" hspace=\"10\" height=\"350\" border=\"0\" align=\"left\" src='".$row['image']."'></a></figure>";
}

?>

and info.php code:

<?php
$servername = "localhost";
$username = "root";
$password = "";

$conn = mysql_connect($servername, $username, $password);
$db = mysql_select_db("doggy");


if(!$conn || !$db)
{
exit(mysql_error());	
}

$result = mysql_query(" SELECT * FROM dog ");
mysql_close();
$row = mysql_fetch_array($result)

 $link=$_GET['id'];
        if ($link == $row[id]){
			 $nr="1";
			 
			 echo "<img style=\"margin-right:10px\" width=\"300\" hspace=\"10\" height=\"350\" border=\"0\" align=\"left\" src='".$row['image']."'>>";
        }
        
		
            ?> 

Sorry for my english

what problem?

i see the image, when i click on image i go to page info.php?id=%272%27 and the page not works
The id must be 1 from DB

you have quoted the id value in the URL, hence '2' is not the same as 2 and the DB doesn’t find it.

In info.php there is no point querying all of the ‘dog’ table and then looping through to match just one id. Instead i would do a ‘where’ on the query

$result = mysql_query(" SELECT * FROM dog WHERE id = '$link' ");

you need to set the $link variable before that though
’ $link=$_GET[‘id’];’

But this is vulnerable to sql injection so you have to ‘sanitize’ the variable. You should really be using mysqli (note the ‘i’) over mysql but for your example you should at least do this

’ $link= mysql_real_escape_string($_GET[‘id’]);’

You then don’t need the while loop on the query. If you count the num of rows returned you can then do various things such as output a message if no result

$rows_returned = mysql_num_rows($result);

You can then do

if($rows_returned > 0){ echo '...

hth

1 Like

For fear of repeating what has been said many times before, you really should not be using the mysql_ functions now as these have been out-of-date for a good many years. You should be using mysqli_ or PDO.

2 Likes

oh guys, i am totally confused
what me to do now?
help me to translate my code from mysql to mysqli

I recommend to use PDO. it’s easier to use in the long run.

1 Like

Also in this sequence of code:

$result = mysql_query(" SELECT * FROM dog ");
mysql_close();
$row = mysql_fetch_array($result)

you run the query, then close the connection to the database, then try to retrieve the results. Lose the middle line.

There are plenty of tutorials around on converting from “old” mysql calls to the newer libraries, grab one and start working through it, and come back with any issues that you encounter.

1 Like

Close your connection at end.

Or omit it as PHP will automatically close it on script end anyways.

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