【问题标题】:PHP MySQL Insert arrayPHP MySQL 插入数组
【发布时间】:2015-10-11 04:56:24
【问题描述】:

我在插入此数组中的值时遇到问题。这里的例子:

$arr = json_decode($_POST['dynfields'], true);
    //{"dynfields":{"dynfields[0][DescRepair]":"Desc repair","dynfields[0][NestParts]":"Parts","dynfields[0][EdPrice]":"10","dynfields[0][DateRepair]":"2015-07-20","dynfields[1][DescRepair]":"Desc repair","dynfields[1][NestParts]":"Parts","dynfields[1][EdPrice]":"5","dynfields[1][DateRepair]":"2015-07-20"}}

    foreach ($arr as $key => $fieldArray ) {
        foreach($fieldArray as $k => $v) {
            echo $k . " - " . $v . "<br>"; // result: dynfields[0][DescRepair] - Desc repair
                                            dynfields[0] [NestParts] - Parts
                                            dynfields[0][EdPrice] - 10
                                            dynfields[0][DateRepair] - 2015-07-20
                                            dynfields[1][DescRepair] - Desc repair
                                            dynfields[1][NestParts] - Parts
                                            dynfields[1][EdPrice] - 5
                                            dynfields[1][DateRepair] - 2015-07-20

        }
        //$query = mysqli_query($mysqli, "INSERT INTO repair (DescRepair, NestParts, EdPrice, DateRepair) VALUES ('?', '?', '?', '?')") or die(mysqli_error($mysqli));  
    }

这是我的代码,但我不知道如何在 db 中插入值。你能给我任何建议吗?谢谢。

【问题讨论】:

    标签: php mysql arrays json insert


    【解决方案1】:

    $query = mysqli_query($mysqli, "INSERT INTO repair (DescRepair, NestParts, EdPrice, DateRepair) VALUES ('?', '?', '?', '?')") or die(mysqli_error($mysqli))

    将每个 ? 替换为 {$arr['xxxxx']} 其中 xxxxx 是您的数组键

    确保很好地转义变量以防止 SQL 注入

    提示:您可以使用 PDO 或准备好的语句

    【讨论】:

      【解决方案2】:

      我不是很理解你的代码,但是一开始你的json不好,在post上,这是一个正确的json示例:

      {
      "dynfields": [
          {
              "DescRepair": "Desc repair",
              "NestParts": "Parts",
              "EdPrice": "10",
              "DateRepair": "2015-07-20"
          },
          {
              "DescRepair": "Desc repair",
              "NestParts": "Parts",
              "EdPrice": "5",
              "DateRepair": "2015-07-20"
          }
        ]
      }
      

      然后您可以使用 dynfields 数据进行 foreach:

      $myvar = json_decode($json,true);
      $data = $myvar['dynfields'];
      
      
          foreach(array_keys($data) as $index){
              var_dump($data[$index]);
          }
      

      然后你会得到这样的东西(var_dump):

       array (size=4)
            'DescRepair' => string 'Desc repair' (length=11)
            'NestParts' => string 'Parts' (length=5)
            'EdPrice' => string '10' (length=2)
            'DateRepair' => string '2015-07-20' (length=10)
       array (size=4)
            'DescRepair' => string 'Desc repair' (length=11)
            'NestParts' => string 'Parts' (length=5)
            'EdPrice' => string '5' (length=1)
            'DateRepair' => string '2015-07-20' (length=10)
      

      【讨论】:

      • 我知道 json 的结构很糟糕,但我没有找到另一种解决方案来从输入中获取价值并发送到 php 代码并在数据库中记录。如果您有更好的解决方案,我将很高兴看到它jsfiddle.net/1ox3Ltfr/1。谢谢。
      • 将您的字段放入表单中,然后当您单击显示按钮时执行以下操作: var formData = $("#formId").serialize();您已经通过 AJAX 将其发送到服务器,然后您将收到一个正确的数组,您可以转换为 JSON 或不
      猜你喜欢
      • 2011-11-10
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2012-10-15
      • 2015-12-04
      • 1970-01-01
      相关资源
      最近更新 更多