我真的很难在我的系统中保存我的这个循环我需要问他们需要多少样式,例如他们选择了3 ..那里会出现3个表,每个你需要写一些数量的表..
我在这里保存我的代码
$numgen = $_POST['numgen']; //number of style (i chose 3 style)
$x= 1;
while($x <= $numgen)
{
$elements = array();
foreach ($_POST['barcode'.$x] as $barcode)
{
$stmt = $db->prepare("SELECT * FROM productmaterialnumber where materialnumber = :bid");
$stmt->execute(array(':bid' => $barcode)) or die(print_r($db->errorInfo(), true));
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$productmaterialnumberID = $row['productmaterialnumberID'];
$matnum = $row['materialnumber'.$x];
$color = $row['color'];
$size = $row['size'];
$productgenericnumberID = $row['productgenericnumberID'];
$productstylecodeID = $row['productstylecodeID'];
$qty = $_POST[$matnum];
$totalqty += $qty;
$matnummm[] = $matnum."<br>";
$quantttt[] = $qty."<br>";
$sizzeeee[] = $size."<br>";
$colooor[] = $color."<br>";
}
$stmt = $db->prepare("INSERT INTO barcoderequest(reqID, buyerID, qty, materialnumber, ID, printstatus, division,
purrequest, dateadded, vendorID, remarks)
VALUES(:field0, :field1, :field2, :field3, :field4, :field5, :field6, :field7, :field8, :field9, :field10)");
$stmt->execute(array(':field0' => $defaultreqnum, ':field1' => $userID,
':field2' => $qty, ':field3' => $matnum, ':field4' => $ID, ':field5' => "Pending", ':field6' => $gennum.$division,
':field7' =>"Pending", ':field8' =>$currentdatetime, ':field9' => $vendor, ':field10' => $remarks))
or die(print_r($db->errorInfo(), true));
}
$x++;
}
如果您发现我在$x
$POST['barcode'.$x]
旁边插入了$matnum = $row['materialnumber'.$x]
,那么我可以知道该ID但它不起作用但是当我删除$x
时它会保存3次,因为我选择3种风格。
有人能帮助我吗?感谢
答案 0 :(得分:0)
$ _ POST ['条形码'。$ x] =&gt;不是一个集合,所以你不能循环它。
试试这个:
<?php
$_POST['numgen'] = 3;//For testing only (Needs to be removed!)
$numgen = $_POST['numgen'];
$_POST["barcode1"] = "1234566789"; //For testing only (Needs to be removed!)
$_POST["barcode2"] = "987654321"; //For testing only (Needs to be removed!)
$_POST["barcode3"] = "147852369"; //For testing only (Needs to be removed!)
$x= 1;
while($x <= $numgen)
{
foreach ($_POST as $key => $value) { //Loop over post instead of individual barcodes
$x="";
$barcode="";
if (strstr($key, 'barcode')) {
$x = str_replace('barcode', '', $key); //Get barcode number
$barcode = $value; // Get barcode
}
if ( $barcode != "") {
echo "Barcode".$x." :".$barcode."<br/>";//For testing only (Needs to be removed!)
$stmt = $db->prepare("SELECT * FROM productmaterialnumber where materialnumber = :bid");
$stmt->execute(array(':bid' => $barcode)) or die(print_r($db->errorInfo(), true));
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$productmaterialnumberID = $row['productmaterialnumberID'];
$matnum = $row['materialnumber'.$x];
$color = $row['color'];
$size = $row['size'];
$productgenericnumberID = $row['productgenericnumberID'];
$productstylecodeID = $row['productstylecodeID'];
$qty = $_POST[$matnum];
$totalqty += $qty;
$matnummm[] = $matnum."<br>";
$quantttt[] = $qty."<br>";
$sizzeeee[] = $size."<br>";
$colooor[] = $color."<br>";
}
$stmt = $db->prepare("INSERT INTO barcoderequest(reqID, buyerID, qty, materialnumber, ID, printstatus, division,
purrequest, dateadded, vendorID, remarks)
VALUES(:field0, :field1, :field2, :field3, :field4, :field5, :field6, :field7, :field8, :field9, :field10)");
$stmt->execute(array(':field0' => $defaultreqnum, ':field1' => $userID,
':field2' => $qty, ':field3' => $matnum, ':field4' => $ID, ':field5' => "Pending", ':field6' => $gennum.$division,
':field7' =>"Pending", ':field8' =>$currentdatetime, ':field9' => $vendor, ':field10' => $remarks))
or die(print_r($db->errorInfo(), true));
$x++;
}
}
}
?>