0

i want to insert multiple input in the same table using php and sql

example:

<script language="javascript">
fields = 0;
function addInput() {
if (fields != 10) {
document.getElementById('text').innerHTML += "<input type='text' name='order'><br/>";
fields += 1;
} else {
document.getElementById('text').innerHTML += "<br />Only 10 insert fields allowed.";
document.form.add.disabled=true;
}
}
</script>


<form action='insert.php' method='post'>
<input type='text' name='order'>


<div id="text">

</div>

<br /><input type='button' value='add new input' onclick='addInput()'> <input type='submit' value='submit'>

</form>

in this code i have unlimited input, i have to insert it in:

dbtable = clients_order

row name : order

++++++++++++++++++
+  id  +  order  +
++++++++++++++++++

so when i use this code :

$order=$_POST['order'];
foreach ($order as $insert_order) {

mysql_query("INSERT INTO  clients_order (order)
    VALUES ('$insert_order')");

 }

Error message : Warning: Invalid argument supplied for foreach()

please help me to do that

thank you

1 Answer 1

2

The foreach() is complaining that $order isn't an array. If you use order[] as the name field in the HTML for your field, then PHP will turn them all into an array for you.

You should also check you have an array (use is_array()) before giving it to a foreach() loop. And you should call mysql_real_escape_string() over each piece of text from $orders in order to prevent SQL injection attacks.

Sign up to request clarification or add additional context in comments.

2 Comments

good, now how can i insert 2 input in one foreach function example : foreach ($order,$another as $insert_order,$another) {
Ideally, you need a generator, but PHP doesn't have those (though there are ways to do it anyway). Instead, you need to look over each array separate and re-assemble the data in another array. Then you loop over that to construct your SQL.

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.