Updating a database entry when checkbox is checked
Posted: Thu Oct 09, 2008 3:16 pm
Maybe it's quite simple problem, but i'm very fresh to php (my first project) and i can't handle it.
So the script is supposed to update the 'display' value from '0' to '1' when a checkbox relevant to this record is checked. Loads of googling and forums searching (and asking) got me up to this stage. Of course, the display value is not being updated. If it would i wouldn't be asking you guys for help (and i need it, deadline for this project is tommorrow noon, and this is the last problem i havn't solved - hopefully)
Save records to database:
Retrieve the data and check checkboxes:
The form can be found on bartosz.dk/pub
The retrieving file is bartosz.dk/pub/manage1.php
Feel free to check it, and please come up with a idiotproof solution, because my brain has been seriously violated by my first encounter to php
So the script is supposed to update the 'display' value from '0' to '1' when a checkbox relevant to this record is checked. Loads of googling and forums searching (and asking) got me up to this stage. Of course, the display value is not being updated. If it would i wouldn't be asking you guys for help (and i need it, deadline for this project is tommorrow noon, and this is the last problem i havn't solved - hopefully)
Save records to database:
Code: Select all
<?php
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//drop existing table
// Create table
mysql_select_db("bartosz_pub", $con);
$sql = "CREATE TABLE football
(
id int NOT NULL AUTO_INCREMENT,
team1 char(25),
team2 char(25),
channel char(15),
date int,
PRIMARY KEY(id),
time varchar(4),
display int
)";
// Execute query
$create = mysql_query($sql,$con);
$counter = count($_POST)/5;
for ($i=1; $i<=$counter; $i++){
print "<tr>";
@$team1 = $_POST['team1'.$i];
print "<td>".$i."</td>";
print "<td>".$team1."</td>";
@$team2 = $_POST['team2'.$i];
print "<td>vs</td>";
print "<td>".$team2."</td>";
@$channel = $_POST['channel'.$i];
print "<td>on</td>";
print "<td>".$channel."</td>";
@$date = $_POST['theDate'.$i];
print "<td>".$date."</td>";
@$time = $_POST['time'.$i];
print "<td>".$time."</td>";
print "</tr>";
$save = "INSERT INTO football(team1,team2,channel,date,time,display) VALUES ('$team1','$team2','$channel','$date','$time','0');";
$execute = mysql_query($save);
}
if($execute)
echo "great";
else
echo "not so great";
mysql_close($con);
?>Code: Select all
<?php
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// select table
mysql_select_db("bartosz_pub", $con);
#Check for whether or not the form has been submitted
if(isset($_POST['sub'])) {
#Form was submitted, handle the update
#loop through the checkboxes and check for selectionm
foreach ($_POST['check'] as $k => $v) {
#Run the update statement for each box that is checked
$sql = "UPDATE football SET display = '1' WHERE id = $v";
$res = mysql_query($sql, $con);
}
#Tell ourselves when the update is done
echo "Update complete";
} else {
#Display the form
mysql_select_db("bartosz_pub", $con);
$result = mysql_query("SELECT * FROM football");
$row = mysql_fetch_assoc($result);
echo "<table border='1'>\n<form action='' name='updForm' method='post' >\n";
#Loop through the results of the query and put them in the table with the checkbox
do {
echo "<tr><td>";
echo $row['id']."</td>\n<td>";
echo $row['team1']."</td>\n<td>";
echo $row['team2']."</td>\n<td>";
echo $row['channel']."</td>\n<td>";
echo $row['date']."</td>\n<td>";
echo $row['time']."</td>\n<td>";
echo $row['recid']."</td>";
echo "<td><input name='check[]' type='checkbox' value='".$row['recid']."' /></td>\n</tr>\n";
} while ($row = mysql_fetch_assoc($result));
echo "<tr><td><input name='sub' type='submit' value='Update Items' /></td></tr>";
echo "</form>\n</table>";
}
?>
The retrieving file is bartosz.dk/pub/manage1.php
Feel free to check it, and please come up with a idiotproof solution, because my brain has been seriously violated by my first encounter to php