在php中将多个表单数据插入数据库

时间:2016-05-04 11:53:13

标签: php

我想插入这些数据集,以便在单击按钮时立即添加到数据库表中。 请帮忙

<form  action="" method="post">                     
0           
    <input type="hidden" name="item_name" value="HandBag">
    <input type="hidden" name="item_description" value="Black Handbag">
    <input type="hidden" name="quantity" value="4">
    <input type="hidden" name="amount" value="3500.00">                 

1           
    <input type="hidden" name="item_name" value="Neclace Set">
    <input type="hidden" name="item_description" value="Amaizing Green Ocean Beads Neclace Set">
    <input type="hidden" name="quantity" value="1">
    <input type="hidden" name="amount" value="2999.00">                 

2           
    <input type="hidden" name="item_name" value="Pants">
    <input type="hidden" name="item_description" value="Beige Ankle Cropped Pants">
    <input type="hidden" name="quantity" value="1">
    <input type="hidden" name="amount" value="2500.00">                 


    <input type = "hidden" name = "token" value = "fa31004463eabefa9010eb5d01816913">

    <input type="submit" name="pay now" value="pay" />

2 个答案:

答案 0 :(得分:0)

最好的方法是使用像

这样的数组名称
<form  action="" method="post">                     
0           
    <input type="hidden" name="item_name[]" value="HandBag">
    <input type="hidden" name="item_description[]" value="Black Handbag">
    <input type="hidden" name="quantity[]" value="4">
    <input type="hidden" name="amount[]" value="3500.00">                 

1           
    <input type="hidden" name="item_name[]" value="Neclace Set">
    <input type="hidden" name="item_description[]" value="Amaizing Green Ocean Beads Neclace Set">
    <input type="hidden" name="quantity[]" value="1">
    <input type="hidden" name="amount[]" value="2999.00">                 

2           
    <input type="hidden" name="item_name[]" value="Pants">
    <input type="hidden" name="item_description[]" value="Beige Ankle Cropped Pants">
    <input type="hidden" name="quantity[]" value="1">
    <input type="hidden" name="amount[]" value="2500.00">                 


    <input type = "hidden" name = "token[]" value = "fa31004463eabefa9010eb5d01816913">

    <input type="submit" name="pay_now" value="pay" />

php代码将是那样的

if(isset($_POST['pay_now']){
  $sth = $dbh->perpare('insert into table(item_name, item_description, quantity, amount) values(?, ?, ?, ?)');

  foreach($_POST['item_name'] as $key=>$val){
    //here is the values of the first item
    $item_name=$val;
    $item_description=$_POST['item_description'][$key];
    $quantity=$_POST['quantity'][$key];
    $amount=$_POST['amount'][$key];
    //insert into database like that
    $sth->execute([$item_name,  $item_description, $quantity, $amount]);
  }
}

答案 1 :(得分:0)

您可以使用php代码,并形成下面提到的方式。

<?php
    if($_POST){
        foreach($_POST['item'] as $key=>$value) {
            $itemname = $value;
            $desc = $_POST['item_description'][$key];
            $qty = $_POST['quantity'][$key];
            $amount = $_POST['amount'][$key];
        }
    }
?>

<form  action="" method="post">                     

<input type="hidden" name="item_name[]" value="HandBag">
<input type="hidden" name="item_description[]" value="Black Handbag">
<input type="hidden" name="quantity[]" value="4">
<input type="hidden" name="amount[]" value="3500.00">                 


<input type="hidden" name="item_name[]" value="Neclace Set">
<input type="hidden" name="item_description[]" value="Amaizing Green Ocean Beads Neclace Set">
<input type="hidden" name="quantity[]" value="1">
<input type="hidden" name="amount[]" value="2999.00">                 


<input type="hidden" name="item_name[]" value="Pants">
<input type="hidden" name="item_description[]" value="Beige Ankle Cropped Pants">
<input type="hidden" name="quantity[]" value="1">
<input type="hidden" name="amount[]" value="2500.00">                 


<input type = "hidden" name = "token" value = "fa31004463eabefa9010eb5d01816913">

<input type="submit" name="pay now" value="pay" />

由于 阿米特