2016-10-02 87 views
0

使用带有Ajax负载的数组插入将多个产品插入单个id。也许使用爆炸,但需要一个清晰的指令,这种类型的输出: enter image description here使用php和PDO插入数组值插入到mysql数据库中的单个ID

所有产品(问题)将输入到一个单一的ID。这些问题列表由Ajax添加。

<?php 
if(isset($_POST['Submit'])){ 
    try { 
    $serviceTitle = $_POST['serviceTitle']; 
    $price = $_POST['price']; 
    $quantity = $_POST['quantity']; 
    $amount = $_POST['amount']; 

    $date = date('Y-m-d'); 
    date_default_timezone_set('Asia/Dacca'); 
    $time = date('h:i:s'); 
    $createBy = $_SESSION['sess_username']; 

    $_SESSION['orderNo'] = $_POST['orderNo']; 
    $_SESSION['customerNo'] = $_POST['customerNo']; 

    if($_POST['Submit']==='Submit'){ 
     for($i=0;$i<count($serviceTitle);$i++){ 
      $statement = $db->prepare("INSERT INTO invoice (orderNo,customerNo,productName,price,quantity,amount,createDate,createTime,createBy) VALUES (?,?,?,?,?,?,?,?,?)"); 
      $statement->execute(array($_POST['orderNo'],$_POST['customerNo'],$serviceTitle[$i],$price[$i],$quantity[$i],$amount[$i],$date,$time,$createBy)); 
     } 
    } 
    header("location: order_confirm_tech_step2.php"); 
    } 
    catch(Exception $e) { 
      $error_message = $e->getMessage(); 
    } 
} 
?> 

它会插入在同一orderNo和customerNo但我想插入数据行是这样的: enter image description here 在此先感谢。

+0

完全不相干的问题,但我想指出,“数量”的普遍接受的速记版应该是“数量”,而不是“Qnt” –

回答

0

您好我认为最好的办法就是像'new_screen_price' 'old_screen_price'

所有inpunt给予不同的名称,如果你的表格是由JS生成,你可以做到这一点,如果不把直接在HTML中你有

<input name='new_screen_price'> 
<input name='new_screen_qty'> 
... 

你把你的后一个值在PHP像

$newScreen = []; 
$newScreen['name'] = 'new screen'; 
$newScreen['price'] = $_POST['new_screen_price']; 

放你的阵列产品中的一个

$products = []; 
$products['newScreen'] = $newScreen; 

您准备好您的要求,我只是对两个参数的价格和名称做,但oviously你需要的所有

foreach($products as $prod){ 
    $statement = $db->prepare("INSERT INTO invoice (orderNo,customerNo,productName,price,quantity,amount,createDate,createTime,createBy) VALUES (?,?,:name,:price,...)"); 
    $statement->bindParam(':name', $prod->name); 
    $statement->bindParam(':price', $prod->price); 
    $statement->execute(); 
} 

良好的一天,我希望这可以帮助你

+0

那么,如何声明“orderNo,customerNo,createDate,createTime,createBy”的值? –