简体   繁体   中英

PHP/MySQL Display single record from MySQL Table

I am having some problems with my PHP code. I have one php file that displays a list of records from MySQL database and I want it when user clicks on a record they get further detail on that record. I have created the link to the second php file but I keep getting "Data Error" when I click on any record.

I have code in the php that checks if the User_ID is a number and I have checked the database set up to make sure the field is formatted as an INT (it is a foreign key as well so the number is unique).

The php code from the first file is:

<table>


<?php 
// Connects to your Database 
mysql_connect("server","username","password") or die(mysql_error()); 
mysql_select_db("databsename") or die(mysql_error()); 
$data = mysql_query("SELECT `User_ID`,`Market_Name`, `Days`, `Time`,`Image`  FROM `markets_info`ORDER BY `Market_Name`; ")  
or die(mysql_error()); 
echo "<tr><th>Name</th><th>Day</th><th>Time</th><th>Image</th></tr>";

while($info = mysql_fetch_array( $data )) 

{  
echo "<tr>"; 
echo "<td><a href=mktdetails.php?id=$info[User_ID]>$info[Market_Name]</a></td> "; 
echo "<td>".$info['Days'] . " </td> "; 
echo "<td>".$info['Time'] . " </td> "; 
echo  "<td>".'<img src="' . $info['Image'] . '" height="80" width="150" />'."</td> ";
echo"</tr>"; 


} 


?> 

</table>

mktdetails.php is:

<table style="width:860px" border="1" class="auto-style15">

<?php 
// Connects to your Database 
mysql_connect("server","username","password") or die(mysql_error()); 
mysql_select_db("databse") or die(mysql_error()); 

$id=$_GET["User_ID"]; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}

$data=mysql_query("SELECT * FROM `markets_info` WHERE `User_ID`=$id");
$row=mysql_fetch_object($data);
echo mysql_error();

echo"<tr><td>$row->Market_Name</td></tr>";
echo"<tr><td>$row->Days</td></tr>";
echo"<tr><td>$row->Time</td></tr>";
echo"<tr><td>$row->Description</td></tr>";


?> 
</table>

It seems that you don't pass via GET 'User_ID'

echo "<td><a href=mktdetails.php?id=$info[User_ID]>$info[Market_Name]</a></td> "; 

Should be something like:

echo "<td><a href='mktdetails.php?User_ID=".$info[User_ID]."'>".$info[Market_Name]."</a></td> "; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM