Note that there are some explanatory texts on larger screens.

plurals
  1. POPOSTing Checkboxes with Submit button to MySQL database
    primarykey
    data
    text
    <p>Currently, I have a piece of code which displays four checkboxes and allows a user to select the checkboxes, click submit, and the data, through the POST method, will be sent to the database (called "Spreadsheet") where it would be stored.</p> <p>Typically, with a radio button, the data stored is only one element. But I noticed that with checkboxes, the elements (for my case specifically) could range from zero to four items. So my issue with my code is, only one element gets stored, even if I press all four. I think I have to store the items as an array, but how do I store + retrieve said items to and from the database? </p> <p>Below is my code: </p> <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;!-- tells browser this is an HTML document --&gt; &lt;head&gt; &lt;!-- container of all head elements --&gt; &lt;/head&gt; &lt;body&gt; &lt;!-- Begin the content of the document --&gt; &lt;? if (isset($_POST['formSubmit2'])){ $category = $_POST['category']; $accountID = $_POST['accountID']; mysql_query("UPDATE Spreadsheet SET category='$category' WHERE accountID='$accountID'"); } while($row = mysql_fetch_array($query)){ $values = array('0 - Luxury','1 - Brand','2 - Retailer','3 - B2B'); ?&gt; &lt;form name ="category" method ="POST" action ="" &gt; &lt;? echo "&lt;input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' &gt;"; for($i = 0; $i &lt; count($values); $i++){ ?&gt; &lt;input type="checkbox" name="category" value="&lt;?php echo $values[$i]; ?&gt;" id="rbl_0" &lt;? if($row['category'] == $i) echo "checked='checked'"; ?&gt;/&gt; &lt;? echo $values[$i] ?&gt;&lt;br&gt; &lt;? } ?&gt; &lt;input type ="Submit" name ="formSubmit2" value ="Submit" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    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.
 

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