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 all the buttons only check out the very last entry in the table. I'm not exactly sure what is causing this problem. If you could take a look at my code below and see if you could figure it out I'd appreciate it. 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)) { $title = $data['title']; $category = $data['category']; $status = $data['status']; $date = date("Y/m/d"); $username = $_COOKIE['cis_username']; $sql = "UPDATE `software` SET `status` = 'Checked Out', `by` = '$username', `date` = '$date' WHERE `title` = '$title'"; echo "<tr bgcolor='#003C64'> <td width='50%'>".$title."</td> <td width='20%'>".$category."</td> <td width='15%'>".$status."</td> <td width='15%'>"; if ($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'])) { 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted February 3, 2007 Share Posted February 3, 2007 Try doing echo mysql_num_rows($query); and make sure that it is more than 1... Also I think mysql_query("$sql") should be mysql_query($sql) Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 3, 2007 Share Posted February 3, 2007 Change: $query = mysql_query("SELECT id, title, category, status FROM software"); to: $query = mysql_query("SELECT id, title, category, status FROM software") or die(mysql_error()); Then see what error you are getting. Quote Link to comment Share on other sites More sharing options...
Rocketaka Posted February 3, 2007 Author Share Posted February 3, 2007 I did what both of you suggestion and so far nothing. It's still the same. I'm receiving no errors. It' just not working like it should. I did the mysql_num_rows and it displayed the correct amount of rows in my table so I have no clue why every button I push will only check out the last entry in the table. It may have to do with the while() loop but I don't know. Quote Link to comment Share on other sites More sharing options...
corbin Posted February 3, 2007 Share Posted February 3, 2007 try <?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']; $category = $data['category']; $status = $data['status']; $date = date("Y/m/d"); $username = $_COOKIE['cis_username']; $sql = "UPDATE `software` SET `status` = 'Checked Out', `by` = '$username', `date` = '$date' WHERE `title` = '$title'"; if(isset($_POST['checkout'])) { mysql_query($sql) or die (mysql_error());} echo "<tr bgcolor='#003C64'> <td width='50%'>".$title."</td> <td width='20%'>".$category."</td> <td width='15%'>".$status."</td> <td width='15%'>"; if ($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'])) { 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
Rocketaka Posted February 3, 2007 Author Share Posted February 3, 2007 Yeah I tried what you suggested and instead of just the last entry in the table getting checked out it will check out everything. Quote Link to comment Share on other sites More sharing options...
alpine Posted February 3, 2007 Share Posted February 3, 2007 The problem is that you are constantly overwriting the $sql variable while the loop is running Try something like this: <?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='hidden' name='id' value='$data['id']'> <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']; $id = $_POST['id']; $sql = "UPDATE `software` SET `status` = 'Checked Out', `by` = '$username', `date` = '$date' WHERE `id` = '$id'"; 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
Rocketaka Posted February 3, 2007 Author Share Posted February 3, 2007 I tried your code and it got an error because for the hidden input field the value was $date['id'] so i made that into a variable called id and it ended up working. Thank you very much! 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.