Solved

php, mysql, checkboxes, update

Posted on 2007-04-06
12
315 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:evcr
[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
  • 6
12 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 18867978
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
 
LVL 7

Author Comment

by:evcr
ID: 18868036
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
 
LVL 20

Expert Comment

by:steelseth12
ID: 18868057
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 7

Author Comment

by:evcr
ID: 18868099
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
 
LVL 20

Expert Comment

by:steelseth12
ID: 18868102
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
 
LVL 7

Author Comment

by:evcr
ID: 18868123
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
 
LVL 7

Author Comment

by:evcr
ID: 18868141
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
 
LVL 20

Expert Comment

by:steelseth12
ID: 18868158
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
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 18868160
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
 
LVL 7

Author Comment

by:evcr
ID: 18868178
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
 
LVL 20

Expert Comment

by:steelseth12
ID: 18868203
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
 
LVL 7

Author Comment

by:evcr
ID: 18868231
cool, this is not the sort of info you get by googling. :-)
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

734 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