Note that there are some explanatory texts on larger screens.

plurals
  1. POgetting multiple values from checkboxes and storing in DB php
    text
    copied!<p>Having trouble storing multiple checkbox values into DB. Not sure if this is the most efficient way but it's the only thing I could come up with. If there are better ways to do it please share. I am still in test mode so there is no validation yet.</p> <p>ITEMS COMING FROM DB</p> <pre><code> $get_products = $db-&gt;prepare("select * from item where user_id = '$id' ORDER BY add_date"); $get_products-&gt;execute(); while ($row = $get_products-&gt;fetch()) { $item_id = $row['item_id']; $user_id = $row['user_id']; $item_name = $row['item_name']; $products .= "&lt;br/&gt;&lt;input type='checkbox' name='items[]' value='$item_id' /&gt;$item_name"; } </code></pre> <p>FORM</p> <pre><code>&lt;form method="post" action="confirm.php"&gt; &lt;?php echo $products; ?&gt; &lt;input type="submit" value="Add Items" id="add_items"/&gt; &lt;input name="from_id" type="hidden" value="1"&gt; &lt;input name="to_id" type="hidden" value="2"&gt; &lt;input type="submit" name="submit" value="Submit" &gt; &lt;/form&gt; </code></pre> <p>PROCESS</p> <pre><code>if(isset($_POST['submit'])) { $from = $_POST['from_id']; $to = $_POST['to_id']; $items = $_POST['items']; if(empty($items)) { $message = "no items in items"; exit; } foreach($items as $i) { $items .= $i. "|"; } json_encode($items); $sql = $db-&gt;prepare("INSERT into trans(from_id, to_id,items)VALUES(:from_id, :to_id, :items"); $sql-&gt;bindValue('from_id', $from); $sql-&gt;bindValue('to_id', $to); $sql-&gt;bindValue('items', $items); if($sql-&gt;execute()) { header("Location: profile.php?user=1"); exit(); } </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