Jump to content

[SOLVED] Another while() problem


Rocketaka

Recommended Posts

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>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/36865-solved-another-while-problem/
Share on other sites

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.

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>";
}
?>

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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.