Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating mysql table using checkbox
    primarykey
    data
    text
    <p>i try to update a mysql table with multiple check box, but my code doesn't seem to work, so any help is welcome. My code is:</p> <pre><code>&lt;strong&gt;Update &lt;strong class="highlight"&gt;multiple&lt;/strong&gt; &lt;strong class="highlight"&gt;rows&lt;/strong&gt; &lt;strong class="highlight"&gt;in&lt;/strong&gt; &lt;strong class="highlight"&gt;mysql&lt;/strong&gt;&lt;/strong&gt;&lt;br&gt; &lt;?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="db_test"; // Database name $tbl_name="test_table"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); // Count table &lt;strong class="highlight"&gt;rows&lt;/strong&gt; $count=mysql_num_rows($result); ?&gt; &lt;table width="500" border="0" cellspacing="1" cellpadding="0"&gt; &lt;form name="form1" method="post" action="&lt;? echo $_SERVER['REQUEST_URI']; ?&gt;"&gt; &lt;tr&gt; &lt;td&gt; &lt;table width="500" border="0" cellspacing="1" cellpadding="0"&gt; &lt;tr&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Id&lt;/strong&gt;&lt;/td&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/td&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Phone Number&lt;/strong&gt;&lt;/td&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Operation&lt;/strong&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php while($rows=mysql_fetch_array($result)) { ?&gt; &lt;tr&gt; &lt;td align="center"&gt;&lt;input type="hidden" name="id[]" value="&lt;? echo $rows['id']; ?&gt;" /&gt;&lt;? echo $rows['id']; ?&gt;&lt;/td&gt; &lt;td align="center"&gt;&lt;input name="name&lt;? echo $rows['id']; ?&gt;" type="text" id="name" value="&lt;? echo $rows['name']; ?&gt;"&gt;&lt;/td&gt; &lt;td align="center"&gt;&lt;input name="email&lt;? echo $rows['id']; ?&gt;" type="text" id="email" value="&lt;? echo $rows['email']; ?&gt;"&gt;&lt;/td&gt; &lt;td align="center"&gt;&lt;input name="description&lt;? echo $rows['id']; ?&gt;" type="text" id="description" value="&lt;? echo $rows['description']; ?&gt;"&gt;&lt;/td&gt; &lt;td align="center"&gt;&lt;input name="phone_number&lt;? echo $rows['id']; ?&gt;" type="text" id="phone_number" value="&lt;? echo $rows['phone_number']; ?&gt;"&gt;&lt;/td&gt; &lt;td align="center"&gt;&lt;input name="operation&lt;? echo $rows['id']; ?&gt;" type="text" id="operation" value="&lt;? echo $rows['operation']; ?&gt;"&gt;&lt;/td&gt; &lt;td align="center"&gt;&lt;input name="ONOFF&lt;? echo $rows['id']; ?&gt;" type="checkbox" id="ONOFF" value="1" &lt;?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;tr&gt; &lt;td colspan="4" align="center"&gt;&lt;input type="submit" name="Submit" value="Submit"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/form&gt; &lt;/table&gt; &lt;?php // Check if button name "Submit" is active, do this if($Submit) { foreach($_POST['id'] as $id) { $onoff = 0; if (isset($_POST["ONOFF".$id])) { $onoff = 1; } $sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'"; $result1=mysql_query($sql1); } } if($result1){ header("location:test_update2.php"); } mysql_close(); ?&gt; </code></pre> <p>Thanks for you help.</p> <p>Regards.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload