通过复选框更新mysql中的选定行或所有行

I´m a newbie in PHP and MySQL. So I stuck on a maybe simple Problem. Now i have read 2 nights about my problem and i found a lot of topics here and over google where´s talking about that but nothings realy help me. I hope someone here can help me.

I have a checkbox that sends a yes/no value to mysql for the field "what". But the problem is i don´t get the id of the selected checkbox.

MySQL looks like ->

id | first_name |second_name | age ?|what  
 1 | first      | second     | 30   | no  
 2 | first      | second     | 35   | no  
 3 | first      | second     | 40   | no  

My Code looks like ->

Select Part:

$query = mysql_query("SELECT * FROM people") or sqlerr(__FILE__, __LINE__);

Update Part:

if (!empty($_GET['look'])) {                

$what = ($_GET['what'] == yes) ? "yes" : "no";
$sql = "UPDATE people SET what = '$what' WHERE id = " . mysql_real_escape_string($id) . "";   
mysql_query($sql) or die(mysql_error()); }

Submit Part:

  <div>             
  <input type="submit" name="look" value="Select">                                  
  <br><br>
  <input type="button" onClick="select_all('what', '1');" value="All">
  <input type="button" onClick="select_all('what', '0');" value="None"> 
  </div>    

Select All/None is controlled by Javascript.

Checkbox Part:

  <td align="center">
   <input type="checkbox" name="what" value="yes" <?php ($what["what"]=="yes"?" checked":"") ?> \>                               
  </td>

When i make a echo to $sql

$sql = "UPDATE people SET what = '$what' WHERE id = " . mysql_real_escape_string($id) . "";   
echo($sql);
exit();  

It shows:
When unselected:

UPDATE people SET what = 'no' WHERE id = 

When selected:

UPDATE people SET what = 'yes' WHERE id = 

This is my JS Part:

 var formblock;
 var forminputs;

 function prepare() {
          formblock= document.getElementById('form_id');
          forminputs = formblock.getElementsByTagName('input');
          }

 function select_all(name, value) {
      for (i = 0; i < forminputs.length; i++) 
      {
       var regex = new RegExp(name, "i");
       if (regex.test(forminputs[i].getAttribute('name'))) {
       if (value == '1') {
           forminputs[i].checked = true;
           } else {
           forminputs[i].checked = false;
          }
        }
      }
    }

 if (window.addEventListener) {
     window.addEventListener("load", prepare, false);
    } else 
 if (window.attachEvent) {
     window.attachEvent("onload", prepare)
    } else 
 if (document.getElementById) {
     window.onload = prepare;
    }

So i think that the Upate SET works but i don´t get the id from the selected checkbox and in MySQL there are also no changes.

Is there anybody could tell me what i´m doing wrong?

THX for your interest!

StefanW

First you should know that checkbox's are only sent to the PHP script via $_POST / $GET if they are actually checked. So knowing this you dont actually need to know the value is yes or no, you can assume that from its existance or not $_POST / $_GET.

So if you place the id of the tables row that relates to this checkbox into the value="" then you have the id of the row to be updated when you look at $_GET['what']

So the PHP update code could become:

if ( isset($_GET['what']) ) {
    $id = mysql_real_escape_string($_GET['what']);
    $sql = "UPDATE people 
              SET what = 'yes'
              WHERE id = $id";

    $res = mysql_query($sql);
    if ( !$res ) {
        // show error
    }
}

Now if you have more that one checkbox you want to call what as I assume from the javascript you need to name these checkboxes as an array like

<input type="checkbox" name="what[]" value="<?php echo $id;?>"> 

See the name="what[]", this will cause you to receive an array of what's in $_GET/$_POST which you can process like

foreach ( $_GET['what'] as $idx => $what ) {
    if ( isset($what) ) {
        $id = mysql_real_escape_string($what);
        $sql = "UPDATE people 
                  SET what = 'yes'
                  WHERE id = $id";

        $res = mysql_query($sql);
        if ( !$res ) {
            // show error
        }
    }
}