duandu8202 2018-06-19 09:59 采纳率: 0%
浏览 91
已采纳

将JSON数据插入MySql

I have a solution with PHP as server-side, Vue JS for front-end and MySQL as DB.

The UI bundles data as JSON and posts it to PHP through axios, and PHP in turn will decode the JSON and inserts into MySQL.

Here is my PHP code (omitting the other lines like connecting etc.):

$data = file_get_contents("php://input"); 
$jsonData = json_decode($data, true); 

//echo var_dump($jsonData);    
// Below is the jsonData as dumped
    //[{"candidate_id":"SM_009","FirstName":"test","LastName":"dummy","DOB":"1990-06-05"}]

$tableName = 'profile';  
foreach((array)$jsonData as $id=>$row) {

      $insertPairs = array();
      foreach ((array)$row as $key=>$val) {          
          $insertPairs[addslashes($key)] = addslashes($val);         
      }      
      $insertKeys = '`' . implode('`,`', array_keys($insertPairs)) . '`';
      $insertVals = '"' . implode('","', array_values($insertPairs)) . '"';
      $sql = "INSERT INTO `{$tableName}` ({$insertKeys}) VALUES ({$insertVals});" ;
      //echo var_dump($sql);
      $stmt = $con->prepare($sql);
      $stmt->execute();   
}

However, here is the actual insert statement generated, which is obviously wrong.

INSERT INTO `profile` (`0`) VALUES ("[{\"candidate_id\":\"SM_009\",\"FirstName\":\"test\",\"LastName\":\"dummy\",\"DOB\":\"1990-06-05\"}]");

Where am I doing wrong? Any help would be greatly appreciated..

Thanks

Note: When I use the same dumped jsondata as hardcoded string, it works.

$data ='[{"candidate_id":"SM_009","FirstName":"test","LastName":"dummy","DOB":"1990-06-12"}]';
//$data = file_get_contents("php://input");

...

Generated statement:

 "INSERT INTO `profile` (`candidate_id`,`FirstName`,`LastName`,`DOB`) VALUES ("SM_009","test","dummy","1990-06-12");"
  • 写回答

2条回答 默认 最新

  • dongshan8953 2018-06-19 11:34
    关注

    The reason you are still receiving the json in your insert statement is because you decoded the first part of your json string and received the data array which still contains the json string inside of it. To resolve this just decode the $jsonData variable again like so:

    <?php 
    $data = file_get_contents("php://input"); 
    $jsonData = json_decode($data, true);
    $jsonData = json_decode($jsonData['data'], true); //Decode the data as well 
    
    $tableName = 'profile';
    foreach((array)$jsonData as $id => $row){
        $insertPairs = array();
        foreach ((array)$row as $key=>$val) {
          $insertPairs[addslashes($key)] = addslashes($val);
        }
        $insertKeys = '`' . implode('`,`', array_keys($insertPairs)) . '`';
        $insertVals = '"' . implode('","', array_values($insertPairs)) . '"';
        $sql = "INSERT INTO `{$tableName}` ({$insertKeys}) VALUES ({$insertVals});" ;
        $stmt = $con->prepare($sql);
        $stmt->execute(); 
    }
    

    You can check out a working example here: https://ideone.com/i86iVP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料