princeofpersia Posted April 4, 2011 Share Posted April 4, 2011 Hi guys, in code below im listing 6 books where user can reserve the books by pressing a submit button, the query works fine except it only chooses last book listed, if i press on book id 1 it runs the query with last book listed, could you help me to see what im doing wrong? thanks in advance <?php //we start a session here to help us pass the user login variable to other pages of the webs application while user is logged in. session_start(); //connect to database include 'include/db.php'; ?> <!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=iso-8859-1" /> <title>Business Events</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="topPan"> <ul> <li><a href="#">login</a></li> <li class="register"><a href="login.php" class="register">resister</a></li> </ul> <a href="index.html"><img src="images/logo.gif" alt="Business Events" width="281" height="56" border="0" class="logo" title="Business Events" /></a> </div> <div id="headerPan"> <div id="headerleftPan"><img src="images/blank.gif" alt="" width="1" height="1" /></div> <div id="headermiddlePan"> <div id="menuPan"> <ul> <li class="home">Home</li> <li><a href="#">About Us</a></li> <li><a href="#">Services</a></li> <li><a href="#">Support</a></li> <li><a href="#">Testimonials</a></li> <li class="contact"><a href="#" class="contact">Contact</a></li> </ul> </div> <div id="headerbodyPan"> <div id="headerleftredPan"> <h2>whatsnew <br /> <span>in 2006</span></h2> <p><span class="largetext">Praesent quis ipsum. ut dui tell</span> dapibus vitae,vehicula vitaeaculis anteftr congue vel,risus.Aenean a pede.Sed..re fringilla,quam utfacilisis consequat dtrer.</p> </div> <div id="headermiddleredPan"><img src="images/blank.gif" alt="" /></div> <div id="headermorePan"><a href="#">know more</a></div> </div> </div> <div id="headerrightPan"><img src="images/blank.gif" alt="" width="1" height="1" /></div> </div> <div id="bodyPan"> <?php //we get the seesion here $myemail = $_SESSION['EmailAddress']; //now, we can get the users name from db in accordance to our session and also we can see if users is logged in using our db $getuser=mysql_query("SELECT * FROM member WHERE EmailAddress='$myemail' AND loggedin='1'"); //here we get number of rows which matches our database if ($getrows=mysql_num_rows($getuser)>=1){ //now from the results we get the first name of users logged in while ($row=mysql_fetch_array($getuser)){ $username=$row['FirstName']; $userid=$row['Id']; } //here we get the credit for user $getcredit=mysql_query("SELECT Credit FROM member WHERE Id='$userid'"); while($row=mysql_fetch_array($getcredit)){ $credit=$row['Credit']; } echo "Welcome $username, Your credit is $credit | <a href='logout.php'>Logout</a>"; ?> <p> <? //From here we start processing the rest of the code //we get list of all books available $getbooks=mysql_query("SELECT * FROM bookstorent"); while($row=mysql_fetch_array($getbooks)) { //here booking proccess starts $bookid=$row['id']; $booktitle=$row['title']; $bookauthor=$row['author']; $bookisbn=$row['isbn']; $bookdescription=$row['description']; $bookreleasedate=$row['releasedate']; $bookcategory=$row['category']; $bookcost=$row['cost']; $bookimage=$row['imagelink']; $bookquantity=$row['quantity']; echo"<form name='book' action='' method='post'>"; //here we list all the data we have from database echo " <table width='746' height='297' border='0'> <tr> <td width='200' height='291' valign='top'><table width='200' height='351' border='0'> <tr> <td><img name='' src='$bookimage' width='200' height='263' alt='my library' /></td> </tr> </table></td> <td width='530' valign='top'><table width='535' border='0'> <tr> <td width='525'>Title: <strong>$booktitle</strong></td> </tr> <tr> <td>Author: <strong>$bookauthor</strong></td> </tr> <tr> <td> Release Date: <strong>$bookreleasedate</strong></td> </tr> <tr> <td>Category: <strong>$bookcategory</strong></td> </tr> <tr> <td>ISBN: <strong>$bookisbn</strong></td> </tr> <tr> <td>Cost: <strong>£$bookcost</strong></td> </tr> <tr> <td>Available to book:<strong>$bookquantity</strong></td> </tr> <tr> <td height='88' valign='top'>Description: <strong>$bookdescription</strong></td> </tr> <tr> <td height='22' valign='top'><input type='submit' name='book' value='Book Now'/></td> </tr> </table></td> </tr> </table> <hr size='1' align='center' width='100%' color='red'> "; echo "</form>"; } ////////////////////////////// ////////////////////////////// //here booking proccess starts ////////////////////////////// ////////////////////////////// if ($_POST['book']){ $bookfrom=date('d-m-Y'); $returndate=date('d-m-Y', strtotime("+3 days")); $bookstatus='out'; $book=mysql_query("INSERT INTO rent (bookid,userid,booktitle,bookfrom,bookuntill,bookstatus) VALUES ('$bookid','$userid','$booktitle','$bookfrom','$returndate','$bookstatus')"); $getcredit=mysql_query("UPDATE member SET Credit=$credit-1 WHERE Id='$userid' "); echo "Book $booktitle has been reserved to be picked up, your pick up date is $bookfrom and return date is $returndate "; } } else { header("Location: users/login.php"); } ?> </p> <!--HTML form starts here--> </div> <div id="bodybottomPan"> <div id="bottomleftPan"> <h2>about services <br /> <span>dapibus sit amet, aliquet</span></h2> <ul> <li><a href="#">Dapibus vitae,vehicula vitaea</a></li> <li><a href="#">Anteftr congue vel,risus.</a></li> <li><a href="#">Pede.fringilla,quam utfacilisis</a></li> <li><a href="#">Consequat dtrer.</a></li> </ul> <p class="more"><a href="#">want to know more solutions</a></p> </div> <div id="bottomrightPan"> <h2>about services <br /> <span>dapibus sit amet, aliquet</span></h2> <ul> <li><a href="#">Dapibus vitae,vehicula vitaea</a></li> <li><a href="#">Anteftr congue vel,risus.</a></li> <li><a href="#">Pede.fringilla,quam utfacilisis</a></li> <li><a href="#">Consequat dtrer.</a></li> </ul> <p class="more"><a href="#">want to know more solutions</a></p> </div> </div> <div id="footermainPan"> <div id="footerPan"> <ul> <li><a href="#">Home</a>| </li> <li><a href="#">About Us</a>| </li> <li><a href="#">Services</a>| </li> <li><a href="#">Support</a>| </li> <li><a href="#">Testimonials</a>| </li> <li><a href="#">Contact</a></li> </ul> <p class="copyright">À?business events. All right reserved.</p> <div id="footerPanhtml"><a href="http://validator.w3.org/check?uri=referer" target="_blank">XHTML</a></div> <div id="footerPancss"><a href="http://jigsaw.w3.org/css-validator/check/referer" target="_blank">css</a></div> <ul class="templateworld"> <li>design by:</li> <li><a href="http://www.templateworld.com" target="_blank">Template World</a></li> </ul> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/232638-need-help-with-listing/ Share on other sites More sharing options...
spiderwell Posted April 4, 2011 Share Posted April 4, 2011 you are not passing a bookid in the <form> why not add a hidden input to pass the bookid, and because you dont pull that from the form, when you send the form, the value of $bookid is the one from the last row of record set, not from the form. I hope that makes sense. I would suggest either putting the form processing before the recordset loop or just not bother with the variables in the recordset, there is no need to do this $bookauthor=$row['author'] <td>Author: <strong>$bookauthor</strong></td> when you can just have <td>Author: <strong>$row['author']</strong></td> or even better post the form to a seperate page, then once processed, redirect back to book listing page. Quote Link to comment https://forums.phpfreaks.com/topic/232638-need-help-with-listing/#findComment-1196547 Share on other sites More sharing options...
princeofpersia Posted April 4, 2011 Author Share Posted April 4, 2011 sorry im a newbie, how do i do that? Quote Link to comment https://forums.phpfreaks.com/topic/232638-need-help-with-listing/#findComment-1196549 Share on other sites More sharing options...
spiderwell Posted April 4, 2011 Share Posted April 4, 2011 this should get you started: at the top of your <form>, add this line into the echo statement <input type='hidden' name='bookid' value='$bookid'> then after the if($_POST) statement add this $bookid = $_POST['bookid'] do the same for all variables you need to pass or you will see soon as you do this, $booktitle's value is the last one in the recordset, not the one clicked on , highlighting the same error you have with bookid currently that should be the quickest way to get the script working without a huge reworking of the code inplace. I would suggest though that you look at refining it somewhat in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/232638-need-help-with-listing/#findComment-1196553 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.