VinceGledhill Posted May 12, 2012 Share Posted May 12, 2012 Hi People. Here is a form that is supposed to read from my database. My primary key is called "index" and I have about 300 records in the database. I wanted to be able to view the records but when I load this page the "php" part is empty. I simply get given a blank form. I know it is connecting to the DB OK because I changed one letter in the password and then got a "can't connect to the database error" just to test it out. <?php $host = 'localhost'; $usr = "the_db_user"; $password = 'not_shared_on_forum'; $db_name = 'stranded'; function cr($string){ $clean_string = str_replace("rn","<BR>",$string); return $clean_string; } //$id = 11; if (!isset($id)) $id = $_GET['id']; mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error()); mysql_select_db ("$db_name") or die ('Error Selecting DB:<br>'.mysql_error()); $read_query = "select * from HelperFormData where index = '$id'"; $results = mysql_query($read_query); $rs = mysql_fetch_array($results); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- body,td,th { font-size: 12px; } --> </style></head> <body> Surname: <? echo $rs["surname"]; ?> <table width="500" border="1" bordercolor = "#2c2cf6" textcolor = "#2c2cf6"cellspacing="1" cellpadding="2"> <tr> <td colspan="5">Record number: <? echo $rs["index"]; ?></td> </tr> <tr> <td width="120" bgcolor = #E6F8EB>Surname</td> <td width="120" bgcolor = #E6F8EB>First Name</td> <td width="120" bgcolor = #E6F8EB>phone1</td> <td width="120" bgcolor = #E6F8EB>phone2</td> <td width="120" bgcolor = #E6F8EB>Location</td> </tr> <tr> <td bgcolor = #E6F8EB textcolor = #AC1636><strong><? echo $rs["surname"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["firstname"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["phone1"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["phone2"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["location"]; ?></strong></td> </tr> <tr> <td bgcolor = #dbeff8>Qualifications</td> <td bgcolor = #dbeff8>Expertise</td> <td bgcolor = #dbeff8>Assistance</td> <td bgcolor = #dbeff8>Languages</td> <td bgcolor = #dbeff8>E-Mail</td> </tr> <tr> <td bgcolor = #dbeff8><strong><? echo $rs["qualifications"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["expertise"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["assistance"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["languages"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["e_mail"]; ?></strong></td> </tr> <tr> <td bgcolor = #E6F8EB>Consent</td> <td bgcolor = #E6F8EB>Published</td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> </tr> <tr> <td bgcolor = #E6F8EB><strong><? echo $rs["consent"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["radio_callsign"]; ?></strong></td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> </tr> <tr> <td colspan="5"><p>Comments:<br /> <strong><? echo $rs["comments"]; ?></strong></td> </tr> </table> </body> </html> </html> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 12, 2012 Share Posted May 12, 2012 What do you get when you do a 'view source' of the page in your browser? Quote Link to comment Share on other sites More sharing options...
VinceGledhill Posted May 12, 2012 Author Share Posted May 12, 2012 This <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- body,td,th { font-size: 12px; } --> </style></head> <body> Surname: <table width="500" border="1" bordercolor = "#2c2cf6" textcolor = "#2c2cf6"cellspacing="1" cellpadding="2"> <tr> <td colspan="5">Record number: </td> </tr> <tr> <td width="120" bgcolor = #E6F8EB>Surname</td> <td width="120" bgcolor = #E6F8EB>First Name</td> <td width="120" bgcolor = #E6F8EB>phone1</td> <td width="120" bgcolor = #E6F8EB>phone2</td> <td width="120" bgcolor = #E6F8EB>Location</td> </tr> <tr> <td bgcolor = #E6F8EB textcolor = #AC1636><strong></strong></td> <td bgcolor = #E6F8EB><strong></strong></td> <td bgcolor = #E6F8EB><strong></strong></td> <td bgcolor = #E6F8EB><strong></strong></td> <td bgcolor = #E6F8EB><strong></strong></td> </tr> <tr> <td bgcolor = #dbeff8>Qualifications</td> <td bgcolor = #dbeff8>Expertise</td> <td bgcolor = #dbeff8>Assistance</td> <td bgcolor = #dbeff8>Languages</td> <td bgcolor = #dbeff8>E-Mail</td> </tr> <tr> <td bgcolor = #dbeff8><strong></strong></td> <td bgcolor = #dbeff8><strong></strong></td> <td bgcolor = #dbeff8><strong></strong></td> <td bgcolor = #dbeff8><strong></strong></td> <td bgcolor = #dbeff8><strong></strong></td> </tr> <tr> <td bgcolor = #E6F8EB>Consent</td> <td bgcolor = #E6F8EB>Published</td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> </tr> <tr> <td bgcolor = #E6F8EB><strong></strong></td> <td bgcolor = #E6F8EB><strong></strong></td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> </tr> <tr> <td colspan="5"><p>Comments:<br /> <strong></strong></td> </tr> </table> </body> </html> </html> Regards VinceG Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 12, 2012 Share Posted May 12, 2012 The reason I asked what the view source is, is because you are using php's lazy-way short opening tags and they are usually not enabled. You need to ALWAYS use full opening tags <?php, so that your php code will always be seen as being php code. There's at least three four different things in your current code that could prevent data from being retrieved from the database - 1) Requesting the page without any $_GET['id'] or the $_GET['id'] is not a number (putting an empty value into the query or a value that will be treated as a zero.) 2) The query is failing due to an error of some kind. 3) Requesting the page with a $_GET['id'] that does not exist (the query matched zero rows.) 4) You have a record with empty data values in your database table. You don't have any logic in your code to handle the first three possibilities and each of those three possibilities would also be producing different php errors in your current code (someone suggested in one of your recent threads to have php's error_reporting and display_errors set to get php to help you.) Your code needs to ALWAYS validate external data and check for errors at each step that can fail. The following is some suggested logic that would tell you why your code is not doing what you expect (I specifically didn't correct a problem in it that is likely the reason for no data being retrieved, since you need to develop some troubleshooting skills of your own.) Every select query you form and execute needs to use this same basic logic - <?php // The following goes right after your database connection/database select code ... $id = isset($_GET['id']) ? intval($_GET['id']) : false; // condition external numerical integer input (if you want a default value to be used, replace the 'false' keyword with that default value) if($id <= 0){ // not a valid id, handle that condition here... echo "The page cannot be displayed, the requested id is invalid."; } else { // valid id (numerical and > 0) $read_query = "select * from HelperFormData where index = $id"; if(!$results = mysql_query($read_query)){ // query failed due to an error, handle that condition here... echo "The page cannot be displayed, due to a database problem.<br />"; echo "Query: $read_query<br />Error: " . mysql_error(); } else { // query executed, test if it matched a row(s) if(!mysql_num_rows($results)){ // no matching row, handle that condition here... echo "The page cannot be displayed, the query did not match any row(s)."; } else { // matched a row, retrieve and use the data here.... $rs = mysql_fetch_array($results); // you are expecting only one row (i.e. no while(){} loop needed.) echo "The requested id does exist, here's the page with the data ..."; } } } edit: added the 4th possible reason. Quote Link to comment Share on other sites More sharing options...
VinceGledhill Posted May 13, 2012 Author Share Posted May 13, 2012 Thanks I've added that code to mine and I now get this error on each query I do... The page cannot be displayed, due to a database problem. Query: select * from HelperFormData where index = 200 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index = 200' at line 1 Surname: Any ideas? Here is the code so far. <?php $host = 'localhost'; $usr = "user"; $password = 'password'; $db_name = 'stranded'; function cr($string){ $clean_string = str_replace("rn","<BR>",$string); return $clean_string; } //$id = 11; if (!isset($id)) $id = $_GET['id']; mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error()); mysql_select_db ("$db_name") or die ('Error Selecting DB:<br>'.mysql_error()); // code from PFMaBiSmAd here // The following goes right after your database connection/database select code ... $id = isset($_GET['id']) ? intval($_GET['id']) : false; // condition external numerical integer input (if you want a default value to be used, replace the 'false' keyword with that default value) if($id <= 0){ // not a valid id, handle that condition here... echo "The page cannot be displayed, the requested id is invalid."; } else { // valid id (numerical and > 0) $read_query = "select * from HelperFormData where index = $id"; if(!$results = mysql_query($read_query)){ // query failed due to an error, handle that condition here... echo "The page cannot be displayed, due to a database problem.<br />"; echo "Query: $read_query<br />Error: " . mysql_error(); } else { // query executed, test if it matched a row(s) if(!mysql_num_rows($results)){ // no matching row, handle that condition here... echo "The page cannot be displayed, the query did not match any row(s)."; } else { // matched a row, retrieve and use the data here.... $rs = mysql_fetch_array($results); // you are expecting only one row (i.e. no while(){} loop needed.) echo "The requested id does exist, here's the page with the data ..."; } } } // code from the man above $read_query = "select * from HelperFormData where index = '$id'"; $results = mysql_query($read_query); $rs = mysql_fetch_array($results); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- body,td,th { font-size: 12px; } --> </style></head> <body> Surname: <? echo $rs["surname"]; ?> <table width="500" border="1" bordercolor = "#2c2cf6" textcolor = "#2c2cf6"cellspacing="1" cellpadding="2"> <tr> <td colspan="5">Record number: <? echo $rs["index"]; ?></td> </tr> <tr> <td width="120" bgcolor = #E6F8EB>Surname</td> <td width="120" bgcolor = #E6F8EB>First Name</td> <td width="120" bgcolor = #E6F8EB>phone1</td> <td width="120" bgcolor = #E6F8EB>phone2</td> <td width="120" bgcolor = #E6F8EB>Location</td> </tr> <tr> <td bgcolor = #E6F8EB textcolor = #AC1636><strong><? echo $rs["surname"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["firstname"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["phone1"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["phone2"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["location"]; ?></strong></td> </tr> <tr> <td bgcolor = #dbeff8>Qualifications</td> <td bgcolor = #dbeff8>Expertise</td> <td bgcolor = #dbeff8>Assistance</td> <td bgcolor = #dbeff8>Languages</td> <td bgcolor = #dbeff8>E-Mail</td> </tr> <tr> <td bgcolor = #dbeff8><strong><? echo $rs["qualifications"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["expertise"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["assistance"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["languages"]; ?></strong></td> <td bgcolor = #dbeff8><strong><? echo $rs["e_mail"]; ?></strong></td> </tr> <tr> <td bgcolor = #E6F8EB>Consent</td> <td bgcolor = #E6F8EB>Published</td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> </tr> <tr> <td bgcolor = #E6F8EB><strong><? echo $rs["consent"]; ?></strong></td> <td bgcolor = #E6F8EB><strong><? echo $rs["radio_callsign"]; ?></strong></td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> <td bgcolor = #E6F8EB> </td> </tr> <tr> <td colspan="5"><p>Comments:<br /> <strong><? echo $rs["comments"]; ?></strong></td> </tr> </table> </body> </html> </html> Quote Link to comment Share on other sites More sharing options...
Barand Posted May 13, 2012 Share Posted May 13, 2012 Beware using reserved words as column names. You need to put "index" inside backticks eg `index` http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html Quote Link to comment Share on other sites More sharing options...
VinceGledhill Posted May 13, 2012 Author Share Posted May 13, 2012 Thanks mate, that was the problem. Changed the name index to indics instead and it's now working. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.