dqol6556 2015-07-07 20:49
浏览 54

php为每个循环插入数据两次而不是每行按预期

I have an associative php array that is created using the JSON decode function.

I am attempting to then insert this array row by row into my database.

The values are inserting but I am getting repeats of the last row.

PHP

<?php
include "config.php";


$gambatch =  $_POST["mydata"];
$myjson = json_decode($gambatch,true);
 foreach ($myjson as $row){
        //get the tweet details
        $name = $row[0];
        $points = $row[1];    
    }
 $sql1 = "INSERT INTO testjson(named,points)
            VALUES ('".$name."','".$points."')";

 if(!mysqli_query($con, $sql1))
       {
           die('Error : ' . mysql_error());
       }
if (!mysqli_query($con,$sql1)) {
  die('Error: ' . mysqli_error($con));
}

echo '<br><br><br><br><br><br>';
echo "data added";

The Json is from another page when dumped from php after json decode looks like this :

array(2) { [0]=> array(7) { [0]=> string(6) "dddddd" [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) [6]=> int(0) } [1]=> array(7) { [0]=> string(7) "fffffff" [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) [6]=> int(0) } } 

This is the full array but I am only trying to insert the first two values in each row) in the code above (eventually I want to insert all but I am kept it smaller while getting it to work).

This is also an array with just two entries it will eventually be dynamic and accept many more so the code needs to be flexible enough. I think I am nearly there?

I am getting no PHP errors the problem is the duplicate entries. It either replacing itself or I have a piece of looping code that I have not spotted.

Many thanks in advance for any help

Finally I will be using prepared statements - using this dangerous sql because it is easy to mock up

  • 写回答

1条回答 默认 最新

  • drnmslpz42661 2015-07-08 18:26
    关注

    I got this working after some further reading. I have added the prepared statement insert as well.

    <?php
    include "config.php";
    
    $gambatch =  $_POST["mydata"];
    
    
    $myjson = json_decode($gambatch,true);
    $myLength= count($myjson);
    //echo 'length is '.$myLength.'';
    $query = $con->stmt_init();
    
    
    for ($row = 0; $row <  $myLength; $row++) {
              $name = $myjson[$row][0];
           $points = $myjson[$row][1];
            //$sql1 = "INSERT INTO testjson(named,points)
           //     VALUES ('".$name."','".$points."')";
         $statement = $con->prepare( "INSERT INTO testjson (named,points)  VALUES (?,?) ");
         $statement->bind_param('si', $name, $points);
          $statement->execute();    
    
    
     }
    
    //if (!mysqli_query($con,$sql1)) {
      //die('Error: ' . mysqli_error($con));
    //}
    
    //echo '<br><br><br><br><br><br>';
    //echo "data added";
    //} 
    //mysqli_close($con);
    $statement->close();
    

    I have left old insert as it may help with conversion down the line somewhere.

    A for loop was required using the array row length as the upper bound. This was extracted using the count($yourArray). above is for two columns and potentially unlimited rows.

    The only is issue I have with the code is I think it can be more efficient as I am performing multiple inserts with the execute inside the loop. If someone can solve that I will switch the answer.

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序