donglikuang8145 2017-12-27 07:06
浏览 30

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

This is My PHP code till now I tried

<?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();
    }
}
?>

this is my form code

<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>

Here is my question is I have an input boxes values with comma separated and I want to insert values one by one to the different rows to the database but now all comma separated values are inserting into the single row any idea how to do Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanchouyi6730 2017-12-27 07:11
    关注

    to broad question. you can explode string using explode function

    HTML

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

    PHP

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行