Href link with ID


#1

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


#2

what problem?


#3

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


#4

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


#5

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


#6

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.


#7

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


#8

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


#10

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.


#11

Close your connection at end.


#12

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


#13

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