Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating MySQL table with PHP and checkboxes
    primarykey
    data
    text
    <p>I have a table called products, with an: </p> <p><strong>id</strong>: <em>int, primary key, auto increment</em> <strong>product_name</strong>: <em>varchar</em> <strong>highlighted</strong>: <em>int (1 or 0)</em></p> <p>So, I want the highlighted field to be totally updateable via my admin page, so whenever I get each product's (or row's) datas, I have a checkbox next to the product's name indicating if it's highlighted (1) or not (0).</p> <p>This is my code:</p> <pre><code>&lt;?php $host = "localhost"; $user = "root"; $pass = "usbw"; $db = "test"; mysql_connect($host,$user,$pass); mysql_select_db($db); ?&gt; &lt;?php if(isset($_POST['submit'])) { foreach (isset($_POST['checkBox']) as $id =&gt; $value) { mysql_query("UPDATE products SET highlighted='$value' WHERE id='$id'"); } echo "Updated! &lt;a href='index.php'&gt;Back&lt;/a&gt;"; } else { echo "&lt;form action='index.php' method='POST'&gt;"; $result = mysql_query("SELECT * FROM products"); while($q = mysql_fetch_assoc($result)) { echo "".$q['product_name'].""; if($q['highlighted'] == '1') { echo "&lt;input type='checkbox' checked value='1' name='checkBox['"; ?&gt;&lt;?php echo $q['id']; ?&gt;&lt;?php echo "']'&gt;"; } else { echo "&lt;input type='checkbox' value='1' name='checkBox['"; ?&gt;&lt;?php echo $q['id']; ?&gt;&lt;?php echo "']'&gt;"; } echo "&lt;br&gt;"; } echo "&lt;input type='submit' value='submit' name='submit'&gt;"; echo "&lt;/form&gt;"; } ?&gt; </code></pre> <p>For some reason, I'm getting the following error whenever I try to update the forms:</p> <pre><code>Warning: Invalid argument supplied for foreach() in C:\Users\Badsector\Desktop\USBWebserver v8.5\8.5\root\teszt\index.php on line 15 </code></pre> <p>Thanks for your assistance in advance, I'm totally confused now, I've been struggling for hours now!</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