Rocketaka Posted February 3, 2007 Share Posted February 3, 2007 I'm building a check out system for a project and everything in my code works just fine. The only problem is that when I press the button to check something out it will check out everything and update all the fields when I only want it to update the field where the software title is at. I just started learning PHP and MySQL if I could get any help on this problem I would appreciate it. The code to where I'm displaying the software is below. Thank you! <?php include("check.php"); include("include.php"); if($_SESSION['auth']) { $query = mysql_query("SELECT id, title, category, status FROM software"); echo "<table align='center' cellpadding='2' cellspacing='2' width='100%'> <tr bgcolor='#1469A2'> <td width='50%'><b>Software Title</b></td> <td width='20%'><b>Category</b></td> <td width='15%'><b>Status</b></td> <td width='15%'><b>Check Out?</b></td> </tr>"; while ($data = mysql_fetch_array($query)) { echo "<tr bgcolor='#003C64'> <td width='50%'>".$data['title']."</td> <td width='20%'>".$data['category']."</td> <td width='15%'>".$data['status']."</td> <td width='15%'>"; if ($data['status'] == "Available") { echo "<form method='POST' style='margin:0px;'> <input type='submit' name='checkout' style='border:1px #1469A2 solid;' value='Check Out'> </form>"; } else { echo "<i>Not Available</i>"; } echo "</td></tr>"; if(isset($_POST['checkout'])) { $date = date("Y/m/d"); $username = $_COOKIE['cis_username']; $title = $data['title']; $sql = "UPDATE `software` SET `status` = 'Checked Out', `by` = '$username', `date` = '$date' WHERE `title` = '$title'"; mysql_query("$sql") or die (mysql_error()); echo "You have checked out <b>".$title."</b> successfully"; } } echo "</table>"; } else { echo "You need to login to view this page! <a href='index.php?page=login'>Login here</a>"; } ?> Link to comment https://forums.phpfreaks.com/topic/36858-solved-trouble-with-while/ Share on other sites More sharing options...
jcbarr Posted February 3, 2007 Share Posted February 3, 2007 Your IF statement is inside your while command so it process that part of the script for every row that is returned in the query. When a user clicks to check out something the script process everything again and and the $_POST['checkout'} variable is set so it process the change for ever row returned. Take the if statement and put it at the top of the script after the query but before the while statement. If you want to allow users to check out multiple items at once, I would suggest sending the form data to a separate processing script. Link to comment https://forums.phpfreaks.com/topic/36858-solved-trouble-with-while/#findComment-175865 Share on other sites More sharing options...
Rocketaka Posted February 3, 2007 Author Share Posted February 3, 2007 I actually tried that, moving the if statement around, but it didn't help. Now instead of the button updating every row, it will update nothing. I tried moving it after the while statement and it did the same thing basically. Then I took the $title variable and defined it within the while statement and got it to work. Thank you for the help here's what the code looks like now: <?php include("check.php"); include("include.php"); if($_SESSION['auth']) { $query = mysql_query("SELECT id, title, category, status FROM software"); echo "<table align='center' cellpadding='2' cellspacing='2' width='100%'> <tr bgcolor='#1469A2'> <td width='50%'><b>Software Title</b></td> <td width='20%'><b>Category</b></td> <td width='15%'><b>Status</b></td> <td width='15%'><b>Check Out?</b></td> </tr>"; while ($data = mysql_fetch_array($query)) { $title = $data['title']; echo "<tr bgcolor='#003C64'> <td width='50%'>".$data['title']."</td> <td width='20%'>".$data['category']."</td> <td width='15%'>".$data['status']."</td> <td width='15%'>"; if ($data['status'] == "Available") { echo "<form method='POST' style='margin:0px;'> <input type='submit' name='checkout' style='border:1px #1469A2 solid;' value='Check Out'> </form>"; } else { echo "<i>Not Available</i>"; } echo "</td></tr>"; } echo "</table>"; if(isset($_POST['checkout'])) { $date = date("Y/m/d"); $username = $_COOKIE['cis_username']; $sql = "UPDATE `software` SET `status` = 'Checked Out', `by` = '$username', `date` = '$date' WHERE `title` = '$title'"; mysql_query("$sql") or die (mysql_error()); echo "You have checked out <b>".$title."</b> successfully"; } } else { echo "You need to login to view this page! <a href='index.php?page=login'>Login here</a>"; } ?> Link to comment https://forums.phpfreaks.com/topic/36858-solved-trouble-with-while/#findComment-175870 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.