I have tryed many things.. need simply to retriev values posted from another page put in array and then add the values to a table with unique name
<?php $f1 = $_POST['f1']; $f2 = $_POST['f2']; $f3 = $_POST['f3']; .... $f5 = $_POST['f100']; $Array = array() for (i=0;i<100;i++){ $Array = $f[i] }
then
for(i=0;i<sizeof($Array);i++){ $sql = "INSERT INTO `current stock` (ItemNumber, Stock) VALUES (Itam1, 'Array[i]' )"; } ?>
Advertisement
Answer
Try something like this ($_POST is an array allready, as mentioned by @EL_Vanja):
// Some random values, for demonstration... $_POST['f1'] = 12; $_POST['f2'] ="string"; $_POST['f3'] = 12.12; $_POST['f100'] = rand(0,10); $array = []; foreach($_POST as $key => $value) { $array[$key] =$value; } print("<pre>".print_r($array,true)."</pre>");
will printout:
<pre> Array ( [f1] => 12 [f2] => string [f3] => 12.12 [f100] => 6 ) </pre>
And for the query part:
$sql = "INSERT INTO `current stock` (ItemNumber, Stock) VALUES "; $lastElement = end($array); $i = 1; foreach($array as $arr){ if($arr == $lastElement) { $separator = ";"; } else { $separator = ","; } $sql .= "(Item".$i.", $arr)". $separator; $i++; } print_r($sql);
And the query will look like:
INSERT INTO `current stock` (ItemNumber, Stock) VALUES (Item1, 12),(Item2, string),(Item3, 12.12),(Item4, 7);