php update multiple records using checkboxes

message from wikstov on 20 Jul 2004
how do i get a record update form to update multiple records, where the records
that have to be updated are selected by marking a checkbox?

i''ve already found some feedback on how to do this in asp, but i haven't come
over anything useful in php

all suggestions are extremely welcome... i'm very close to banging my head on
my desk
 
wikstov replied to wikstov on 20 Jul 2004
does anyone know whether this is pssible at all?
thanks a lot in advance
 
wikstov replied to wikstov on 21 Jul 2004
anyone?
 
Gary White replied to wikstov on 21 Jul 2004
Grab the values of the checked boxes and use the IN predicate for your
WHERE clause. Maybe this will get you started:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Demo</title>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (isset($_POST['one']))
$recs[]="'one'";
if (isset($_POST['two']))
$recs[]="'two'";
if (isset($_POST['three']))
$recs[]="'three'";
if (is_array($recs)){
if (count($recs)>1){
$where=join($recs,",");
$sql="UPDATE mytable SET field=value WHERE field IN ($where)";
} else {
$sql="UPDATE mytable SET field=value WHERE field=".$recs[0];
}
echo $sql;
} else
echo "None selected";
exit;
}
?>
<form name="form1" method="post" action="">
<label><input type="checkbox" name="one" value="checkbox"> One</label><br>
<label><input type="checkbox" name="two" value="checkbox"> Two</label><br>
<label><input type="checkbox" name="three" value="checkbox"> Three</label><br>
<input type="submit">
</form>
</body>
</html>

Gary
 
Gary White replied to Gary White on 21 Jul 2004
No time to test right now, but how about something like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Demo</title>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (is_array($_POST['check'])){
if (count($_POST['check'])>1){
$where=join($_POST['check'],",");
$sql="UPDATE mytable SET field=value WHERE field IN ($where)";
} else {
$sql=$_POST['check'];
$sql="UPDATE mytable SET field=value WHERE field=".$sql[0];
}
echo $sql;
} else
echo "None selected";
exit;
}
?>
<form name="form1" method="post" action="">
<label><input type="checkbox" name="check[0]" value="One"> One</label><br>
<label><input type="checkbox" name="check[1]" value="Two"> Two</label><br>
<label><input type="checkbox" name="check[2]" value="Three"> Three</label><br>
<input type="submit">
</form>
</body>
</html>

Gary
 

Archived message: php update multiple records using checkboxes (Macromedia Dreamweaver Web Design)