I am trying to create a list of links on a web page. Each link represents a list item in the database. When a link is clicked the database is called to retrieve the rest of the information from that database item and display it in its own page. I attached two pages to post info to a database. These work. (post02.xhtml and new_post02.php). Plus the (database02.sql) file. A page to view a list of links which when clicked retrieves a row of information from the database and display it in its own page. A list of links is displayed but the links themselves don't function. (view_post02.php). Can someone have a look at this page and tell me what I'm missing? Thanks.
I posted a zip file with all four pages but here is the code. create database animals; use animals; create table mammals ( postid int unsigned not null auto_increment primary key, title char(20) not null, discription char(20) not null ); grant select, insert, update, delete on animals.* to mitchell@localhost identified by 'mpassword'; <!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>post02</title> </head> <body> <table> <form action="new_post02.php" method="post"> <tr> <td>title:</td> <td><input type="text" name="title" size="40" maxlength="40"/> </td> </tr> <tr> <td>discription:</td> <td><input type="text" name="discription" size="40" maxlength="40"/> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="register" name="post" width="99" height="39"/> </td> </tr> </form> </table> </body> </html> <?php //new_post02.php // create short variable names $title=$_POST['title']; $discription=$_POST['discription']; if (!$title || !$discription) { echo "You have not entered all the required details.<br />" ."Please go back and try again."; exit; } if (!get_magic_quotes_gpc()) { $title = addslashes($title); $discription = addslashes($discription); } @ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals'); if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit; } $query = "insert into mammals (title, discription) values ('".$title."', '".$discription."');"; $result = $db->query($query); if ($result) { echo $db->affected_rows." posting inserted into database."; } else { echo "An error has occurred. The item was not added."; } $db->close(); ?> <?php //view_post02.php @ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals'); if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit; } if (isset($_GET['view'])) { $view = $_GET['view']; } // display links 1, 2, and 3 which represent // the postid no. in database. (This works). $query = "SELECT postid FROM mammals ORDER BY postid"; $result = $db->query($query); $num_results = $result->num_rows; for ($j = 0 ; $j < $num_results ; ++$j) { $row = $result->fetch_assoc(); echo "<li><a href='view_post02.php?view=$row[postid]'>$row[postid]</a>"; } // click on link to display new page with database // info for that link. (This don't work). $query = "SELECT * FROM mammals WHERE view='$view'"; $result = $db->query($query); $num_results = $result->num_rows; echo $num_results; $db->close(); ?>
you last query is mistaken. $query = "SELECT * FROM mammals WHERE view='$view'"; you are passing the value postid, right ? so modify the query as $query = "SELECT * FROM mammals WHERE postid='$view'"; cheers.
Thanks for your reply. That might solve some of the problem. When I click on any one of the listed links that were generated, they are supposed to open up a page with the title and description information from that row in the database that correlates to each link. 1 2 3 There is still something missing. Can anyone tell me how to do that? Thanks.
<?php //view_post02.php @ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals'); if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit; } if (!isset($_GET['view'])) { // display links 1, 2, and 3 which represent // the postid no. in database. (This works). $query = "SELECT postid FROM mammals ORDER BY postid"; $result = $db->query($query); $num_results = $result->num_rows; for ($j = 0 ; $j < $num_results ; ++$j) { $row = $result->fetch_assoc(); echo "<li><a href='view_post02.php?view=$row[postid]'>$row[postid]</a>"; } } else { $view = $_GET['view']; // click on link to display new page with database // info for that link. (This don't work). $query = "SELECT * FROM mammals WHERE postid='$view'"; $result = $db->query($query); $data = $result->fetch_assoc(); echo '<strong>Title:</strong> '.$data['title']; echo '<strong>Description:</strong> '.$data['discription']; } $db->close(); ?> PHP:
It works. Thank you very much, now I can sleep better tonight. I don't fully understand what rearranging GET[view] has to do with it, but I will study it tomorrow. Thanks again.
Thanks. I plan to. I have some examples on input filtering, but first I try to learn the main functionality.