1

I don't know how to structure the PHP/MySQL queries to accomplish this. I am trying to add items to a list. I have a form which lists all items with a multiple checkbox form. For every item checked, I need it to create a new entry into a MYSQL table. The table is has three fields, entry_id, list_id and item_id.

So that, if I displayed a list of 5 items

LIST #1

  • Item 1
  • Item 2
  • Item 3
  • Item 4
  • Item 5

And selected 3

LIST #1

  • Item 1
  • Item 2*
  • Item 3*
  • Item 4
  • Item 5*

I need the php/mysql code which will insert those values into the table like so:

  • entry_id / list_id / item_id
  • 1 / 1 / 2
  • 2 / 1 / 3
  • 3 / 1 / 5

Does that make sense?

2 Answers 2

1

Something like this should do it:

<?php
  // get array of selected elements
  $selections = $_POST['selections'];
  $list = intval($_POST['list_id'], 10);
  // prevent sql injection
  $selections = array_map(function ($el) {
    return intval($el, 10);
  }, $selections);
  // create tuples
  $selections = array_map(function ($el) use ($list) {
    return '(' . $list . ', ' . $el . ')';
  }, $selections);
  // create a comma sperated list of tuples
  $selections = implode(', ', $selections);
  $sql = 'INSERT INTO table (list_id, item_id) VALUES ' . $selections . ';';

  // run $sql through the driver of your choice
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Use HTML like the following:

<strong>List Number 1</strong>
<ul>
<li><label><input type="checkbox" name="checked[1][]" value="1">Value 1</label></li>
<li><label><input type="checkbox" name="checked[1][]" value="2">Value 2</label></li>
<li><label><input type="checkbox" name="checked[1][]" value="3">Value 3</label></li>
<li><label><input type="checkbox" name="checked[1][]" value="4">Value 4</label></li>
<li><label><input type="checkbox" name="checked[1][]" value="5">Value 5</label></li>
</ul>

<strong>List Number 2</strong>
<ul>
<li><label><input type="checkbox" name="checked[2][]" value="1">Value 1</label></li>
<li><label><input type="checkbox" name="checked[2][]" value="2">Value 2</label></li>
<li><label><input type="checkbox" name="checked[2][]" value="3">Value 3</label></li>
<li><label><input type="checkbox" name="checked[2][]" value="4">Value 4</label></li>
<li><label><input type="checkbox" name="checked[2][]" value="5">Value 5</label></li>
</ul>

Where the name parameter has a format of checked[X][] where "X" is the List ID.

On submission, with the values mentioned in your question, you would get:

array(1) {
  [1]=>
  array(3) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "3"
    [2]=>
    string(1) "5"
  }
}

So to produce an array like that which you have mentioned in the OP:

$sqlValues = array();
foreach( $_POST['checked'] as $list_id => $values ){
  foreach( $values as $k => $v ){
    $sqlValues[] = ( $k+1 ).' , '.(int) $list_id.' , ' .(int) $v;
  }
}
$sqlCmd = 'INSERT INTO `yourTable` ( `entry_id` , `list_id` , `item_id` )
           VALUES ( '.implode( ' ) , ( ' , $sqlValues ).' )';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.