Forum Moderators: coopster

Message Too Old, No Replies

Update DB using checkbox selection.

         

bodycount

7:59 am on Jun 18, 2008 (gmt 0)

10+ Year Member



I am using the following way to get info back from the database. This will bring back say 5 rows of which i want to update some info to say 3 of the rows but I want to select the three rows using a checkbox. I cant seem to get this to work. Can anyone help please?.

$query1 = "SELECT * FROM boxproduct where company_id = ' ' and FRTYPE = '$FRTYPE' order by boxproduct_id asc LIMIT 0 , 1000;";
$result1 = mysql_query($query1);
$num_rows=mysql_num_rows($result1);
while ($row=mysql_fetch_array($result1))
{

$BOXPRODUCT_id = $row['BOXPRODUCT_id'];
$COMPANY_id = $row['company_id'];

echo "<tr>
<td>$BOXPRODUCT_id</td>
<td>$FRTYPE</td>
<td><input type=\"checkbox\" value=\"$BOXPRODUCT_id\"></td>
</tr>";
}
echo "</table>";

dreamcatcher

3:32 pm on Jun 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Checkbox needs to have a name. Also use the array operators. This will assign all the checkboxes to the $_POST['id'] array;

<input type=\"checkbox\" name=\"id[]\" value=\"$BOXPRODUCT_id\">

When you update, do this:

if (!empty($_POST['id'])) {
mysql_query("UPDATE boxproduct SET something = 'something' WHERE boxproduct_id IN(".implode(",",$_POST['id']).")) or die(mysql_error());
}

Hope that helps.

dc

bodycount

11:48 am on Jun 19, 2008 (gmt 0)

10+ Year Member



Thanks for that. It worked great once i found that there was a " missing

mysql_query("UPDATE boxproduct SET something = 'something' WHERE boxproduct_id IN(".implode(",",$_POST['id']).") missing " ) or die(mysql_error());

So yes thanks again.

dreamcatcher

5:35 pm on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, sorry about that. Glad you got it working ok.

dc