dimanche 2 août 2015

Update selected or all rows in mysql via checkbox

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'])) {                

$markiert = ($_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 =

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




Aucun commentaire:

Enregistrer un commentaire