如何使用逗号分隔值访问输入框值并保存到数据库

时间:2017-12-27 07:06:22

标签: php

这是我的PHP代码,直到现在我试过

<?php
include_once("../include/connClass.php");
$db = new Database();
$conn = $db->getConnection();

if(isset($_POST["save"]))
{
 $date = $_POST["txtDate1"];
}

$date = $_POST["txtDate1"];
$service = $_POST["txtService1"];
$charge = $_POST["txtCharge1"];
$amount = $_POST["txtAmount1"];
$unit = $_POST["txtUnit1"];
$total = $_POST["txtTotal1"];

for ($i = 0; $i <= count($date); $i++) {

  try {
        $sql = $conn->prepare("INSERT INTO backup_master (backup_date, 
backup_service, backup_charge, backup_amount, backup_unit, backup_total)
            VALUES (:backup_date, :backup_service, :backup_charge, 
:backup_amount, :backup_unit, :backup_total)");

        $sql->bindParam(':backup_date', $date);
        $sql->bindParam(':backup_service', $service);
        $sql->bindParam(':backup_charge', $charge);
        $sql->bindParam(':backup_amount', $amount);
        $sql->bindParam(':backup_unit', $unit);
        $sql->bindParam(':backup_total', $total);

        $query = $sql->execute();
        // echo $query;
    }
    catch (PDOException $e) {
        echo $sql . "<br />Error" . $e->getMessage();
    }
}
?>

这是我的表单代码

<form method="POST" action="backend/save.php">

<input type="text" id="txtDate1" name="txtDate1">
<input type="text" id="txtService1" name="txtService1">
<input type="text" id="txtCharge1" name="txtCharge1">
<input type="text" id="txtAmount1" name="txtAmount1">
<input type="text" id="txtUnit1" name="txtUnit1">
<input type="text" id="txtTotal1" name="txtTotal1">

<button type="submit" name="save" class="btn btn-primary">Save</button>
</form>

这是我的问题是我有一个逗号分隔的输入框值,我想逐个将值插入到数据库的不同行但现在 所有逗号分隔的值都插入单行任何想法如何提前谢谢。

1 个答案:

答案 0 :(得分:0)

广泛的问题。你可以使用explode函数

来爆炸字符串

HTML

<input type="text" name="dataString">

<强> PHP

$dataString=$_GET['dataString'];
$testArray = explode(',', $dataString);
print_r($testArray);