Jump to content

[SOLVED] Selecting DB records with checkbox


Kloucek

Recommended Posts

For starters - i'm very fresh to php, so that's why the question posted is pretty noob-like.

 

I have retrieved some data from a DB using a loop, and send the checkbox value to another file.

// select table
mysql_select_db("bartosz_pub", $con);
$result = mysql_query("SELECT * FROM football");
while($row = mysql_fetch_array($result)){
echo "<table border='1'><tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['team1']."</td>";
echo "<td> vs </td>";
echo "<td>".$row['team2']."</td>";
echo "<td> on </td>";
echo "<td>".$row['channel']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['time']."</td>";	
echo "<td><input type='checkbox' name=''check[" . $row['id'] . "]'  ></td></tr>";

}


?>
<form name="send" method="post" action="admin.php">
<tr>
<td colspan="9" align="center"><input type="submit" value="choose"  ></td>
</tr>
</form>
</table>

 

I want to update the database column 'display' from '0' to '1' when a corresponding checkbox is being checked. In admin.php i've put following code:

 

<?php

foreach ( $_POST['check'] AS $id => $check ) {
    mysql_query("update football set display = 1 where id = $id");
}  
?>

 

To be honets i've put it together from various forum topics, tutorials and questions and not fully understand what's happening (told you, i'm a php-noob ;) ). I need to have it done for tmr, as a school project and i really have my brain smashed from all the coding i did so far. If someone would be kind enough to post a ready made code for me, perhaps with some basic explanation what is going on i will be praising him and his descendants up to fourth generation ;)

 

 

You should ALWAYS add ... or die(mysql_error()); after a mysql_query(). It will help you troubleshoot. You should also have values in your checkboxes. Try this for your checkboxes:

echo "<td><input type='checkbox' name='check[]' value='" . $row['id'] . "'></td></tr>";

and your loop:

<?php
foreach ( $_POST['check'] AS $id ) {
    mysql_query("update football set display = 1 where id = $id") or die(mysql_error());
}  
?>

<?php

#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>\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>".$row['id']."</td>\n<td>".$row['team1']."</td>\n<td>".$row['team2']."</td>\n<td>".$row['channel']."</td>\n<td>".$row['date']."</td>\n<td>".$row['time']."</td>\n<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>";
}

?>

 

I did that pretty quick, untested, so give it a shot and see if it works for you. I added a few comments, if they're confusing just ask.

ok, it does things ;) Not sure which though. It doesn't change the 'display' value for sure.

I'll put the whole code i have so far here, and tell you what's the output:

save.php (save the data to database):

<?php
$con = mysql_connect("host:port","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);




?>

 

manage.php - output of the data, and checkboxes:

<?php
$con = mysql_connect("host:port","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
// select table
mysql_select_db("bartosz_pub", $con);


$result = mysql_query("SELECT * FROM football");
while($row = mysql_fetch_array($result)){
echo "<table border='1'><tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['team1']."</td>";
echo "<td> vs </td>";
echo "<td>".$row['team2']."</td>";
echo "<td> on </td>";
echo "<td>".$row['channel']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['time']."</td>";	
echo "<td><input type='checkbox' name=''check[" . $row['id'] . "]'  ></td></tr>";

}


?>
<form name="send" method="post" action="admin.php">
<tr>
<td colspan="9" align="center"><input type="submit" value="choose"  ></td>
</tr>
</form>
</table>

 

and finally admin.php - which shoul be the output file:

<?php

#Check for whether or not the form has been submitted (well, just copy paste what you wrote  )
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>\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>".$row['id']."</td>\n<td>".$row['team1']."</td>\n<td>".$row['team2']."</td>\n<td>".$row['channel']."</td>\n<td>".$row['date']."</td>\n<td>".$row['time']."</td>\n<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 whole project can be found on:

1. www.bartosz.dk/pub (form, and save.php after clicking the submit button)

2. www.bartosz.dk/pub/manage1.php (what we've came up with today)

 

 

<?php

#Check for whether or not the form has been submitted (well, just copy paste what you wrote  )
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>\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>".$row['id']."</td>\n<td>".$row['team1']."</td>\n<td>".$row['team2']."</td>\n<td>".$row['channel']."</td>\n<td>".$row['date']."</td>\n<td>".$row['time']."</td>\n<td><input name='check[]' type='checkbox' value='".$row['id']."' /></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>";
}

?>

 

Try that... I had "recid" for the value of the checkbox, where yours is just "id"

 

So far everything else looks okay.

 

Let me know

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.