Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve imploded array from a cell in an MySQL database through PHP
    text
    copied!<p>Thanks for taking the time to look at this question. </p> <p>Currently, I have a piece of code that creates four checkboxes labeled as "Luxury, Brand, Retailer," and "B2B." I have looked into a number of PHP methods to create checkboxes, and I felt the implode() function was the most simple and suitable for my job. I have looked into a number of tutorials to create the implosions, however, they did not fit my criteria, as I would like the database values be reflected in the front-end. Currently in my database, the implode() works, therefore (for example), if I check "Luxury", "Brand", "Retailer", and press the "Submit" button, the three items "Luxury, Brand, Retailer" will be in that specified cell. It looks like my code works in the back-end, but these are my issues:</p> <ol> <li>I am not exactly sure (despite multiple Googles) how to retrieve those values stored in the single-cell array, and have it selected as "selected" (this would "check" the box in the front-end)</li> </ol> <p>Could someone kindly take a look at my code below and let me know what seems to be missing/wrong/erroneous so I could attempt the revisions? Anything would be appreciated, thank you!</p> <pre><code>&lt;? if (isset($_POST['formSubmit2'])){ $category = mysql_real_escape_string(implode(',',$_POST['category'])); $accountID = $_POST['accountID']; mysql_query("UPDATE Spreadsheet SET category='$category' WHERE accountID='$accountID'"); } $query = mysql_query("SELECT * FROM Spreadsheet LIMIT $firstRow,$rpp"); while($row = mysql_fetch_array($query)){ // Begin Checkboxes $values = array('Luxury','Brand','Retailer','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_&lt;? echo $i; ?&gt;" &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;? } ?&gt; </code></pre>
 

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