Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My example use PDO but I think you get the idea, Also you should move to PDO or MYSQLI instead of mysql</p> <p>the example:</p> <pre><code>&lt;?php // pdo example $sql = 'INSERT INTO table (field1, field2, field3) VALUES (:value1, :value2, :value3)'; // $dbh is pdo connection $insertTable = $dbh-&gt;prepare($sql); $countArray = count($array); for ($i = 0; $i &lt; $countArray; $i++) { $insertTable-&gt;bindParam(':value1', $array['value1'][$i], PDO::PARAM_INT); // if value is int $insertTable-&gt;bindParam(':value2', $array['value2'][$i], PDO::PARAM_STR); // if value is str $insertTable-&gt;bindParam(':value3', $array['value3'][$i], PDO::PARAM_STR); $insertTable-&gt;execute(); } ?&gt; </code></pre> <p><strong>Input name format</strong></p> <p>I see you are doing this: <code>amount_' . $x . '</code> your array post will look like this:</p> <pre><code>[amount_0] =&gt; 100 [amount_1] =&gt; 200 [amount_2] =&gt; 1 [quantity] =&gt; 10 [quantity] =&gt; 20 [quantity] =&gt; 1 </code></pre> <p>but if your write <code>amount[]</code> the array will look like this:</p> <pre><code>[amount] =&gt; Array ( [0] =&gt; 100 [1] =&gt; 200 [2] =&gt; 1 ) [quantity] =&gt; Array ( [0] =&gt; 10 [1] =&gt; 20 [2] =&gt; 1 </code></pre> <p>The last option makes it much better to read the array.</p> <p><strong>MYSQLI example</strong></p> <pre><code>&lt;?php $sql = 'INSERT INTO table (field1, field2, field3) VALUES (?, ?, ?)'; $stmt = $mysqli-&gt;prepare($sql); $countArray = count($array); for ($i = 0; $i &lt; $countArray; $i++) { $stmt-&gt;bind_param('ssd', $array['value1'][$i], $array['value2'][$i], $array['value3'][$i]); $stmt-&gt;execute(); } ?&gt; </code></pre> <p>As you can see there is standing <code>ssd</code> before the parameters these are the types there are 4 types:</p> <ul> <li>i = intenger</li> <li>s = string</li> <li>d = double</li> <li>b = blob</li> </ul> <p>You must always define this.</p> <p><strong>Edit</strong></p> <p>You should use this:</p> <pre><code>&lt;?php // Parse the form data and add inventory item to the system if (isset($_POST['cartOutput'])) { $sql= 'INSERT INTO orders (product_name, price, quantity, date_added) VALUES(?,?,?, NOW())'; $stmt = $myConnection-&gt;prepare($sql); $countArray = count($_POST["item_name"); for ($i = 0; $i &lt; $countArray; $i++) { $stmt-&gt;bind_param('sss', $_POST['item_name'][$i], $_POST['amount'][$i], $_POST['quantity'][$i]); $stmt-&gt;execute(); } echo $sql ; exit(); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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