Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay keywords from DB as Checkbox Options
    text
    copied!<p>I've created a database using mysqli and I have keywords stored in a table called "Keywords". I need to use the keywords stored in this table to display as options on a form in the form of checkboxes.</p> <p>So, if the keyword has not been added to the db, then the checkbox on the form will not display for users to choose from. What's the best way to achieve this? My sql knowledge is basic but I can have an idea on how the query might look. Just not sure about how to display them as checkboxes.</p> <p>UPDATE UPDATE UPDATE! I was able to get my array to display with checkboxes: Here is what I did:</p> <pre><code>&lt;?php include 'db.php'; $sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC"); print &lt;&lt;&lt;HERE HERE; while ($row = mysqli_fetch_array($sql)) { $key = $row['Keyword_Name']; $id = $row["keyID"]; print &lt;&lt;&lt;HERE &lt;input type="checkbox" id="$key"&gt;$key&lt;br /&gt; HERE; } ?&gt; </code></pre> <p>NOW MY QUESTION IS: How do I pass the checked value off to my new table (profileTable) a completely separate table containing the form results?</p> <p>MY FORM CODE LOOKS LIKE THIS (Fri 8/9/13):</p> <pre><code> &lt;div id="form1profile"&gt; &lt;form method="post" enctype="multipart/form-data" name="addProfile" id="addProfile" &gt; &lt;fieldset&gt; &lt;legend&gt;Add/Create a Media Source Profile&lt;/legend&gt; &lt;br&gt; &lt;table width="400" border="0" align="center" cellpadding="0" cellspacing="0"&gt; &lt;tr&gt; &lt;th &gt;&lt;label&gt;First Name&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="FName" type="text" id="FName" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th &gt;Middle Name&lt;/th&gt; &lt;td&gt;&lt;input name="MName" type="text" id="MName" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th &gt;&lt;label&gt;Last&lt;br&gt; Name&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="LName" type="text" id="LName" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Prefix&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="Prefix" type="text" id="Prefix" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Title &lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="Title" type="text" id="Title" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Dept&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="Dept" type="text" id="Dept" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Phone&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="PH1" type="text" id="PH1" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Other Phone&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="PH2" type="text" id="PH2" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Email&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="Email" type="text" id="Email" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label for="ProfileImg"&gt;Photo&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="ProfileImg" type="file" multiple id="ProfileImg" form="addProfile"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th valign="top"&gt;&lt;label class="biotextTitle"&gt;Bio/Info&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;textarea name="bioLinks" cols="55" rows="25" id="bioLinks" placeholder="Paste Bio/Links here..."&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label class="Keywords"&gt;Keyword&lt;/label&gt;&lt;/th&gt; &lt;td&gt; &lt;?php include 'db.php'; $sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC"); foreach ($_POST['keyword'] as $keyword) { if ($keyword) { // $keyword is a selected keyword } } while ($row = mysqli_fetch_array($sql)) { $key = $row['Keyword_Name']; $id = $row["keyID"]; print &lt;&lt;&lt;HERE &lt;input type="checkbox" name="keyword[$key]" id="$key"&gt;$key&lt;br /&gt; HERE; } ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label class="tags"&gt;Tags&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input name="Tags" type="text" id="Tags" size="40"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;input name="SaveBtn" type="submit" id="SaveBtn" formaction="insertProfile.php" formmethod="POST" value="Save"&gt;&lt;/th&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&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