Question : 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("localhost:3306","admin","admin");
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['id']?>]" value="<?=$values['status']?>"></td>
    </tr>
    </tr>
   <?php endforeach; ?>
  </table>
 <input type="submit" value="Update">
</form>
</body>
</html>

Answer : checkbox and mysql

Here zou have it. Tested.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
<?php
 $p='Infos Updated';
$db=mysql_pconnect("localhost:3306","admin","admin");
mysql_select_db("product");
if (isset($_POST['update_button'])) {
  if(isset($_POST['update'])){
    $query1 = "UPDATE product SET status=1 WHERE status=0 and id in(". implode(',',       

$_POST['update']).")";
    $query2 = "UPDATE product SET status=0 WHERE status=1 and id not in(". implode(',',     

$_POST['update']).")";

    if(!mysql_query($query1)){
         echo '<p>Error Updating '.mysql_error().'</p>';
     }
    if(!mysql_query($query2)){
         echo '<p>Error Updating '.mysql_error().'</p>';
     }
   }
   else{
    if(!mysql_query("UPDATE product SET status=0")){
         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" id="update_<?php echo $values['id']?>" name="update[]" value="<?php 

echo $values['id']?>" <?php echo $values['status']==1 ? 'checked' : '' ?>"></td>
    </tr>
    </tr>
   <?php endforeach; ?>
  </table>
 <input type="submit" value="Update" name="update_button">
</form>
</body>
</html>
Random Solutions  
 
programming4us programming4us