如何编写要插入数据库的函数

时间:2018-09-04 05:44:55

标签: php mysql

所以我在php中有此功能,可显示一组表单输入

    function column_display(){
 static $i = 0;
  $i++;
echo'<td><input type="number" name="spent'.$i.'"></td>
            <td><textarea rows="2" placeholder="Enter Bill no. and Bill date" name="bill'.$i.'"></textarea></td>
            <td><textarea rows="2" placeholder="Name of the vendor" name="vendor'.$i.'" maxlength="45"></textarea></td>
            <td><textarea rows="2" placeholder="Additional details of expenditure" name="detail'.$i.'" maxlength="45"></textarea></td>
            <td><textarea rows="2" placeholder="Enter Quantity" name="quantity'.$i.'" maxlength="25"></textarea></td>
            <td><textarea rows="2" placeholder="Remark/Comment" name="remark'.$i.'" maxlength="45"></textarea></td>
          </tr>';

            }

计数器从1到17运行,即17次,我必须发布每个输入来更新我的表。是否有任何我可以循环请求它们并将它们更新到现有表中的快捷方式。 这就是我现在正在做的

UPDATE school_release 

SET item_spent = CASE WHEN item_id = 'it1' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent1'
                  WHEN item_id = 'it2' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent2'               
                  WHEN item_id = 'it3' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent3'
                  WHEN item_id = 'it4' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent4'
                  WHEN item_id = 'it5' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent5'
                  WHEN item_id = 'it6' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent6'
                  WHEN item_id = 'it7' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent7'
                  WHEN item_id = 'it8' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent8'
                  WHEN item_id = 'it9' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent9'
                  WHEN item_id = 'it10' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent10'
                  WHEN item_id = 'it11' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent11'
                  WHEN item_id = 'it12' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent12'
                  WHEN item_id = 'it13' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent13'
                  WHEN item_id = 'it14' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent14'
                  WHEN item_id = 'it15' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent15'
                  WHEN item_id = 'it16' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent16'
                  WHEN item_id = 'it17' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent17'
                  ELSE item_spent
                 END,
SET item_bill =  CASE WHEN item_id = 'it1' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent1'
                  WHEN item_id = 'it2' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent2'               
                  WHEN item_id = 'it3' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent3'
                  WHEN item_id = 'it4' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent4'
                  WHEN item_id = 'it5' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent5'
                  WHEN item_id = 'it6' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent6'
                  WHEN item_id = 'it7' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent7'
                  WHEN item_id = 'it8' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent8'
                  WHEN item_id = 'it9' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent9'
                  WHEN item_id = 'it10' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent10'
                  WHEN item_id = 'it11' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent11'
                  WHEN item_id = 'it12' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent12'
                  WHEN item_id = 'it13' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent13'
                  WHEN item_id = 'it14' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent14'
                  WHEN item_id = 'it15' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent15'
                  WHEN item_id = 'it16' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent16'
                  WHEN item_id = 'it17' AND stamp = '$stamp' AND school_id = '$school_id' THEN '$spent17'
                  ELSE item_spent
                 END,

。 。 。 。 。  SET item_remark =

1 个答案:

答案 0 :(得分:0)

不要将$i放在每个名称中,而是给它们使用数组样式的名称,例如name="bill[]"name="vendor[]"。然后$_POST['bill']$_POST['vendor']将是数组,您可以遍历它们:

$stmt = $conn->prepare("INSERT INTO yourTable (bill, vendor, detail, quantity, remark) VALUES (?, ?, ?, ?, ?)");;
$stmt->bind_param("sssis", $bill, $vendor, $detail, $quantity, $remark);
foreach ($_POST['bill'] as $i => $bill) {  
    $vendor = $_POST['vendor'][$i];
    $detail = $_POST['detail'][$i];
    $quantity = $_POST['quantity'][$i];
    $remark = $_POST['remark'][$i];
    $stmt->execute();
}