php, mysql, checkboxes, update

PHP, MySQL, HTML form with multiple rows and checkboxes

I'm trying to get a form to update itself once it has been submitted to a mysql db. It is a html form containing a checkbox for each record, if the record row = 1 I need the box checked, if it's 0 i need the box unchecked. If I change the checkbox then resubmit I need the database updated and the checkbox unchecked.

The mysql table contains 3 fields: id (0 to 100), reg (aaaaa to zzzzz), names (0 or 1)

A seemingly simple task which I have so far not got working, here is the php page:

[code]
<?
//debug info
print_r($_REQUEST);
mysql_connect('host','user' , 'password');
mysql_select_db('mydb');
//just select the first 20 records
$sql="SELECT * FROM mytable WHERE id >= 0 AND id < 20 ORDER BY id ASC";
$result=mysql_query($sql);
// count table rows
$count=mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table>
<tr><td>ID</td><td>Names</td></tr>
<?
while($row=mysql_fetch_array($result)){
?>
<tr><td><? $id[]=$row['id']; echo $row['id']; ?></td><td>
<?
$names[]=$row['names'];
 if ($row['names'] == '1' || $row['names'] == 'on') {$ch = 'checked="checked"'; }
 else {$ch = '';}
 echo '<input name="names[' . $row['id'] . ']" type="checkbox" id="names[' . $row['id'] . ']" value="' . $row['names'] . '" ' . $ch . '></td>';
?>
</tr>
<?
}
?>
</table>
<input type="submit" name="Submit" value="Submit"></td>
</form>
<?
if(isset($_REQUEST['names'])){
$names = $_REQUEST['names'];
      for($i=0;$i<$count;$i++){
            if (isset($names[$i])) {
                  if ($names[$i] == 'on' || $names[$i] == '1' ) {
                  $sql="UPDATE mytable SET names=\"1\" WHERE id=\"$i\"";
                  }
            }
            else {
                  $sql="UPDATE mytable SET names=\"0\" WHERE id=\"$i\"";
            }
      }
}
$result=mysql_query($sql);
mysql_close();
?>

[/code]

If someone could just edit this code to show me what is wrong it would save me  some time!

thanks
LVL 7
evcrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steelseth12Commented:
hey :)

<?
print_r($_REQUEST);
mysql_connect('host','user' , 'password');
mysql_select_db('mydb');


if(isset($_REQUEST['names'])){

$names = $_REQUEST['names'];


$reff = unserialize($_REQUEST["old_values"]);

      foreach($reff as $key=>$value) {
      
                        
            if($names[$key]!=$value) {
            
      
      
            $sql="UPDATE mytable SET names='".$names[$key]."' WHERE id='".$key."'";
            
            
                         
                  mysql_query($sql) or die(mysql_error());
            }
      
            
      }


}

?>

<?
//debug info

//just select the first 20 records
$sql="SELECT * FROM mytable WHERE id >= 0 AND id < 20 ORDER BY id ASC";
$result=mysql_query($sql);
// count table rows
$count=mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table>
<tr><td>ID</td><td>Names</td></tr>
<?
while($row=mysql_fetch_array($result)){
?>
<tr><td><? $id[]=$row['id']; echo $row['id']; ?></td><td>
<?

$arr[$row['id']] = $row['names'];
$names[]=$row['names'];
 if ($row['names'] == '1' || $row['names'] == 'on') {$ch = 'checked="checked"'; }
 else {$ch = '';}
 echo '<input name="names[' . $row['id'] . ']" type="checkbox" id="names[' . $row['id'] . ']" value="1" ' . $ch . '></td>';
?>
</tr>
<?
}

$send_values = serialize($arr);
?>
<input type="hidden" name="old_values" value='<?=$send_values?>'>
</table>
<input type="submit" name="Submit" value="Submit"></td>
</form>
0
evcrAuthor Commented:
Thanks,

did you test this? I get:

Array()
<br />
<b>Warning</b>:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in <b>C:\Apache2\htdocs\regfinder\test.php</b> on line <b>19</b><br />
<form name="form1" method="post" action="">
<table>
<tr><td>ID</td><td>Names</td></tr>
<br />
<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>C:\Apache2\htdocs\regfinder\test.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>:  Undefined variable:  arr in <b>C:\Apache2\htdocs\regfinder\test.php</b> on line <b>38</b><br />
<input type="hidden" name="old_values" value='N;'>
</table>
<input type="submit" name="Submit" value="Submit"></td>
</form>
0
steelseth12Commented:
Im not able to test it as i dont have a server running in this computer ...  but from the line you are getting the errors  on it seems like you changed the code above.... can you repost the code .
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

evcrAuthor Commented:
well it is running on a server but i can't give you the login, maybe a working example from your end would be easier
0
steelseth12Commented:
No you didnt understand me .... im at an internet cafe right now waiting for an email ... and i dont have an apache running here so i can not  test the code i post .
But from what you posted above .. its seems like you modified the code i posted ...
Can you post the modified code so i can see exactly where the problem is.
0
evcrAuthor Commented:
Cut and paste of code without mysql logins:
<?
print_r($_REQUEST);
mysql_connect('hostname','loginname' , 'password');
mysql_select_db('regfinder');
if(isset($_REQUEST['names'])){
$names = $_REQUEST['names'];
$reff = unserialize($_REQUEST["old_values"]);
      foreach($reff as $key=>$value) {
            if($names[$key]!=$value) {
            $sql="UPDATE reg SET names='".$names[$key]."' WHERE id='".$key."'";            
                  mysql_query($sql) or die(mysql_error());
            }    
      }
}
//just select the first 20 records
$sql="SELECT * FROM reg WHERE id >= 0 AND id < 20 ORDER BY id ASC";
$result=mysql_query($sql);
// count table rows
$count=mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table>
<tr><td>ID</td><td>Names</td></tr>
<?
while($row=mysql_fetch_array($result)){
?>
<tr><td><? $id[]=$row['id']; echo $row['id']; ?></td><td>
<?
$arr[$row['id']] = $row['names'];
$names[]=$row['names'];
 if ($row['names'] == '1' || $row['names'] == 'on') {$ch = 'checked="checked"'; }
 else {$ch = '';}
 echo '<input name="names[' . $row['id'] . ']" type="checkbox" id="names[' . $row['id'] . ']" value="1" ' . $ch . '></td>';
?>
</tr>
<?
}
$send_values = serialize($arr);
?>
<input type="hidden" name="old_values" value='<?=$send_values?>'>
</table>
<input type="submit" name="Submit" value="Submit"></td>
</form>

The HTML return is:
Array
(
    [names] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 1
            [5] => 1
            [6] => 1
            [7] => 1
            [8] => 1
            [9] => 1
            [12] => 1
        )

    [old_values] => a:20:{i:0;s:1:\"1\";i:1;s:1:\"1\";i:2;s:1:\"1\";i:3;s:1:\"1\";i:4;s:1:\"1\";i:5;s:1:\"1\";i:6;s:1:\"1\";i:7;s:1:\"1\";i:8;s:1:\"1\";i:9;s:1:\"1\";i:10;s:1:\"0\";i:11;s:1:\"0\";i:12;s:1:\"0\";i:13;s:1:\"0\";i:14;s:1:\"0\";i:15;s:1:\"0\";i:16;s:1:\"0\";i:17;s:1:\"0\";i:18;s:1:\"0\";i:19;s:1:\"0\";}
    [Submit] => Submit
)
<br />
<b>Notice</b>:  unserialize(): Error at offset 10 of 297 bytes in <b>H:\Apache2\htdocs\regfinder\test.php</b> on line <b>7</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>H:\Apache2\htdocs\regfinder\test.php</b> on line <b>8</b><br />
<form name="form1" method="post" action="">
<table>
<tr><td>ID</td><td>Names</td></tr>
<tr><td>0</td><td>
<input name="names[0]" type="checkbox" id="names[0]" value="1" checked="checked"></td></tr>
<tr><td>1</td><td>
<input name="names[1]" type="checkbox" id="names[1]" value="1" checked="checked"></td></tr>
<tr><td>2</td><td>
<input name="names[2]" type="checkbox" id="names[2]" value="1" checked="checked"></td></tr>
<tr><td>3</td><td>
<input name="names[3]" type="checkbox" id="names[3]" value="1" checked="checked"></td></tr>
<tr><td>4</td><td>
<input name="names[4]" type="checkbox" id="names[4]" value="1" checked="checked"></td></tr>
<tr><td>5</td><td>
<input name="names[5]" type="checkbox" id="names[5]" value="1" checked="checked"></td></tr>
<tr><td>6</td><td>
<input name="names[6]" type="checkbox" id="names[6]" value="1" checked="checked"></td></tr>
<tr><td>7</td><td>
<input name="names[7]" type="checkbox" id="names[7]" value="1" checked="checked"></td></tr>
<tr><td>8</td><td>
<input name="names[8]" type="checkbox" id="names[8]" value="1" checked="checked"></td></tr>
<tr><td>9</td><td>
<input name="names[9]" type="checkbox" id="names[9]" value="1" checked="checked"></td></tr>
<tr><td>10</td><td>
<input name="names[10]" type="checkbox" id="names[10]" value="1" ></td></tr>
<tr><td>11</td><td>
<input name="names[11]" type="checkbox" id="names[11]" value="1" ></td></tr>
<tr><td>12</td><td>
<input name="names[12]" type="checkbox" id="names[12]" value="1" ></td></tr>
<tr><td>13</td><td>
<input name="names[13]" type="checkbox" id="names[13]" value="1" ></td></tr>
<tr><td>14</td><td>
<input name="names[14]" type="checkbox" id="names[14]" value="1" ></td></tr>
<tr><td>15</td><td>
<input name="names[15]" type="checkbox" id="names[15]" value="1" ></td></tr>
<tr><td>16</td><td>
<input name="names[16]" type="checkbox" id="names[16]" value="1" ></td></tr>
<tr><td>17</td><td>
<input name="names[17]" type="checkbox" id="names[17]" value="1" ></td></tr>
<tr><td>18</td><td>
<input name="names[18]" type="checkbox" id="names[18]" value="1" ></td></tr>
<tr><td>19</td><td>
<input name="names[19]" type="checkbox" id="names[19]" value="1" ></td></tr>
<input type="hidden" name="old_values" value='a:20:{i:0;s:1:"1";i:1;s:1:"1";i:2;s:1:"1";i:3;s:1:"1";i:4;s:1:"1";i:5;s:1:"1";i:6;s:1:"1";i:7;s:1:"1";i:8;s:1:"1";i:9;s:1:"1";i:10;s:1:"0";i:11;s:1:"0";i:12;s:1:"0";i:13;s:1:"0";i:14;s:1:"0";i:15;s:1:"0";i:16;s:1:"0";i:17;s:1:"0";i:18;s:1:"0";i:19;s:1:"0";}'>
</table>
<input type="submit" name="Submit" value="Submit"></td>
</form>


0
evcrAuthor Commented:
i'm not sure what the unserialize is supposed to do in this case but>>

 $reff = unserialize($_REQUEST["old_values"]);

old_values wasnt't set in the first place so I don't see what difference that would make
0
steelseth12Commented:
old_values was set here

<input type="hidden" name="old_values" value='a:20:{i:0;s:1:"1";i:1;s:1:"1";i:2;s:1:"1";i:3;s:1:"1";i:4;s:1:"1";i:5;s:1:"1";i:6;s:1:"1";i:7;s:1:"1";i:8;s:1:"1";i:9;s:1:"1";i:10;s:1:"0";i:11;s:1:"0";i:12;s:1:"0";i:13;s:1:"0";i:14;s:1:"0";i:15;s:1:"0";i:16;s:1:"0";i:17;s:1:"0";i:18;s:1:"0";i:19;s:1:"0";}'>
0
steelseth12Commented:
try this

<?
print_r($_REQUEST);
mysql_connect('hostname','loginname' , 'password');
mysql_select_db('regfinder');
if(isset($_REQUEST['names'])){
$names = $_REQUEST['names'];
$reff = base64_decode($_REQUEST["old_values"]);
$reff = unserialize($reff);
      foreach($reff as $key=>$value) {
           
                  if(array_key_exists($key,$names)) {
                  
                  if($names[$key]!=$value) {
            $sql="UPDATE reg SET names='".$names[$key]."' WHERE id='".$key."'";            
                  mysql_query($sql) or die(mysql_error());
            }
                  
                  }else{
                  
                        if($value==1) {
                              $sql="UPDATE reg SET names='0' WHERE id='".$key."'";            
                  mysql_query($sql) or die(mysql_error());
                        
                        
                        }
                  
                  
                  }
                      
      }
}
//just select the first 20 records
$sql="SELECT * FROM reg WHERE id >= 0 AND id < 20 ORDER BY id ASC";
$result=mysql_query($sql);
// count table rows
$count=mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table>
<tr><td>ID</td><td>Names</td></tr>
<?
while($row=mysql_fetch_array($result)){
?>
<tr><td><? $id[]=$row['id']; echo $row['id']; ?></td><td>
<?
$arr[$row['id']] = $row['names'];
$names[]=$row['names'];
 if ($row['names'] == '1' || $row['names'] == 'on') {$ch = 'checked="checked"'; }
 else {$ch = '';}
 echo '<input name="names[' . $row['id'] . ']" type="checkbox" id="names[' . $row['id'] . ']" value="1" ' . $ch . '></td>';
?>
</tr>
<?
}
$send_values = serialize($arr);
$send_values = base64_encode($send_values);
?>
<input type="hidden" name="old_values" value='<?=$send_values?>'>
</table>
<input type="submit" name="Submit" value="Submit"></td>
</form>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evcrAuthor Commented:
yep that works, cheers, looking at you code i'm a bit mystified why you needed to do all that just to check a few boxes?
0
steelseth12Commented:
well as gamebits said in you other treat empty checkbox dont send values ... so an array is created with all the old values. ... it then need to be serialised so it can be send with form .... for some reason that failed to send ... so i encoded it to make sure it is send properly ... we then decode and unserialize and loop through the old values .... these contain alll values even checkbox that are unchecked .... and we compare them with the values of the checkboxes.
0
evcrAuthor Commented:
cool, this is not the sort of info you get by googling. :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.