asaidi
asked on
checkbox and mysql
Hi
i have database table that i displayed for the user to choose a value for status field that maybe can accept two values 0 ou 1
how i can update it to the mysql using php
i can show my script and what i want to do
****
<?php
$p='Infos Updated';
$db=mysql_pconnect("localh ost:3306", "admin","a dmin");
mysql_select_db("product", $db);
if (isset($_POST['update'])) {
foreach ($_POST['update'] as $pkey => $newval) {
$sql = "UPDATE product SET status='$newval' WHERE id='$pkey'";
if (mysql_query($sql)){
echo $p;
$p3=$p;
if ($p==$p3){
$p=' ';
}
else{
echo '<p>Error Updating '.mysql_error().'</p>';
}
}
}
}
$query= "SELECT * FROM product order by type,-seq DESC" ;
// get the rows and put them in an array
$result=mysql_query($query );
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
?>
<table bgcolor='#AAccff' border="0" width='100%'
<tr>
<td id='td' width='15%'>Prod_id</td>
<td id='td' width='30%'>Category</td>
<td id='td' width='34%'>product</td>
<td id='td' width='15%'>Type</td>
<td id='td' width='6%'>Status</td></tr ></table>
<?php
if (mysql_num_rows($result) == 0) {
echo "No rows found";
exit;
}
$myArray=array();
while ($row = mysql_fetch_assoc($result) ){
$myArray[]=$row;}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
<table bgcolor='#AAccAA' border="0" width='100%'>
<?php foreach ($myArray as $values): ?>
<tr>
<td width='15%'><?= $values['prod_id']?></td>
<td width='30%'><?= $values['cat_id']?></td>
<td width='34%'><?= $values['product']?></td>
<td width='15%'><?= $values['type']?></td>
<td><input type="checkbox" checked name="update[<?=$values['i d']?>]" value="<?=$values['status' ]?>"></td>
</tr>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="Update">
</form>
</body>
</html>
i have database table that i displayed for the user to choose a value for status field that maybe can accept two values 0 ou 1
how i can update it to the mysql using php
i can show my script and what i want to do
****
<?php
$p='Infos Updated';
$db=mysql_pconnect("localh
mysql_select_db("product",
if (isset($_POST['update'])) {
foreach ($_POST['update'] as $pkey => $newval) {
$sql = "UPDATE product SET status='$newval' WHERE id='$pkey'";
if (mysql_query($sql)){
echo $p;
$p3=$p;
if ($p==$p3){
$p=' ';
}
else{
echo '<p>Error Updating '.mysql_error().'</p>';
}
}
}
}
$query= "SELECT * FROM product order by type,-seq DESC" ;
// get the rows and put them in an array
$result=mysql_query($query
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
?>
<table bgcolor='#AAccff' border="0" width='100%'
<tr>
<td id='td' width='15%'>Prod_id</td>
<td id='td' width='30%'>Category</td>
<td id='td' width='34%'>product</td>
<td id='td' width='15%'>Type</td>
<td id='td' width='6%'>Status</td></tr
<?php
if (mysql_num_rows($result) == 0) {
echo "No rows found";
exit;
}
$myArray=array();
while ($row = mysql_fetch_assoc($result)
$myArray[]=$row;}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
<table bgcolor='#AAccAA' border="0" width='100%'>
<?php foreach ($myArray as $values): ?>
<tr>
<td width='15%'><?= $values['prod_id']?></td>
<td width='30%'><?= $values['cat_id']?></td>
<td width='34%'><?= $values['product']?></td>
<td width='15%'><?= $values['type']?></td>
<td><input type="checkbox" checked name="update[<?=$values['i
</tr>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="Update">
</form>
</body>
</html>
ASKER
Hi stilliard
thank you very much for your answer
ok when i check the value the value is checked and in database is 1 but sometimes i need to uncheck the value and in database must be replaced to value 0 how i can do this please
thank you very much for your answer
ok when i check the value the value is checked and in database is 1 but sometimes i need to uncheck the value and in database must be replaced to value 0 how i can do this please
Hi
Try to use value as name of the field and use a common nae for all the check boxes and then update them for more help u can refer
http://www.homeandlearn.co.uk/php/php4p11.html
http://www.plus2net.com/php_tutorial/array_checkbox.php
Try to use value as name of the field and use a common nae for all the check boxes and then update them for more help u can refer
http://www.homeandlearn.co.uk/php/php4p11.html
http://www.plus2net.com/php_tutorial/array_checkbox.php
The problem using chcekbox is when you unchcek it, you haven't that variable in post request variables. So you need to go trough query and check if a row that has status value=1 isn't set in post variables and then update the row. Even so with rows that have status=0. It's better to use select box or radiobuttons. But Ican write the solution for you later.
What you could try is loop the update statement the same as you looped the checkboxes in.
Something like this...
<?php
if (isset($_POST['update'])) {
$query= "SELECT * FROM product order by type,-seq DESC" ;
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)){
$newval = ( $_POST['update'][$i] ) ? '1' : '0';
$sql = "UPDATE product SET status='{$newval}' WHERE id='{$row['id']}'";
if (mysql_query($sql)) {
echo $p;
$p3=$p;
if($p==$p3) $p=' ';
}
else echo '<p>Error Updating '.mysql_error().'</p>';
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i havent looked over the code in alot of detail yet,
buti m guesing the $values['status'] var is the value of either 1 or 0 then,
if so, try changing: <input type="checkbox" checked name="update[<?=$values['i
to: <input type="checkbox" name="update[<?=$values['i