Solved

checkbox and mysql

Posted on 2010-08-26
6
399 Views
Last Modified: 2013-12-13
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>
0
Comment
Question by:asaidi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:stilliard
ID: 33537972
Hi,
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['id']?>]" value="<?=$values['status']?>">
to: <input type="checkbox" name="update[<?=$values['id']?>]" value="1"<?php if($values['status']) echo ' checked'; ?>>
0
 

Author Comment

by:asaidi
ID: 33538779
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
0
 
LVL 5

Expert Comment

by:amrish80
ID: 33539022
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Fugas
ID: 33541379
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.
0
 
LVL 6

Expert Comment

by:stilliard
ID: 33541569

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>';
	}
}

Open in new window

0
 
LVL 4

Accepted Solution

by:
Fugas earned 500 total points
ID: 33541783
Here zou have it. Tested.
<?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>

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question