0

I am wanting to do an array for the same user where multiple form data for the same item would be entered into the same table in MySQL.

Page 1

<form method="post" action="order-check-out.php">
<div>Country</div><div><input type="text" name="ef_country[]"></div>
<div>State</div><div><input type="text" name="ef_state[]"></div>
<div>City</div><div><input type="text" name="ef_city[]"></div>

<div>Country</div><div><input type="text" name="ef_country[]"></div>
<div>State</div><div><input type="text" name="ef_state[]"></div>
<div>City</div><div><input type="text" name="ef_city[]"></div>

<input type="submit" value="Order Services">

And the above could be more than 10 inserts.

Check Out page

$_SESSION['ef_country'] = $_POST['ef_country'];
$_SESSION['ef_state'] = $_POST['ef_state'];
$_SESSION['ef_city'] = $_POST['ef_city'];

$db->insert('as_user_addr', array(
"ef_client_id" => $userInfo['user_id'],
"ef_country" => $_SESSION['ef_country'],
"ef_state" => $_SESSION['ef_state'],
"ef_city" => $_SESSION['ef_city'],
));

However, if I have one set of input boxes, the data will insert into MySQL db without any issue. However, I cannot figure out how to do it for multiple form data. Many of the examples found on here seem to echo data, which does not help me. I am needing help with inserting data. I am not sure if it requires a count or loops, etc. I can write PHP the old way, but not this new type of programming.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
  • here's a couple of links to get you started. http://stackoverflow.com/questions/6889065/inserting-multiple-rows-in-mysql http://stackoverflow.com/questions/10262763/php-how-to-loop-through-a-post-array You should really try something out for yourself before asking – andrew Mar 30 '14 at 22:59

1 Answers1

0
for($i = 0; $i < count($_SESSION['ef_city']; $i++){

$db->insert('as_user_addr', array(
"ef_client_id" => $userInfo['user_id'],
"ef_country" => $_SESSION['ef_country'][$i],
"ef_state" => $_SESSION['ef_state'][$i],
"ef_city" => $_SESSION['ef_city'][$i],
));

}
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • Wouldn't it be nicer to insert all the data in a single query? – andrew Mar 30 '14 at 23:07
  • @andrew it doesn't matter much in terms of performance as long as you are not closing / reopening the connection. But sure it would be nicer to insert multiple values at once (though I don't know what framework the OP is using). – Loïc Mar 30 '14 at 23:09
  • I just tried this, and am getting PHP Parse error: syntax error, unexpected ';' for the "for" line. I read the example pages above, and cannot seem to figure it out. I am doing several $db->insert on the same page if that has anything to do with it. @andrew – user3479274 Apr 06 '14 at 03:58
  • I figured it out, it was missing a closed symbol for the session. for($i = 0; $i < count($_SESSION['ef_city']); – user3479274 Apr 06 '14 at 04:18