简体   繁体   English

PHP仅从JSon文件向MySQL插入第一条记录

[英]Php inserts first record only to MySQL from JSon file

I created the JSon file using the c# to export the selected records from the local MySQL database. 我使用c#创建了JSon文件,以从本地MySQL数据库导出所选记录。 The php code is used to insert the JSon file recods to the MySQL remote database. php代码用于将JSon文件记录插入到MySQL远程数据库中。 But the problem is, when I run the php code, it only inserts the first record from the JSon to the database rather than all records in the JSon file. 但是问题是,当我运行php代码时,它仅将JSon中的第一条记录插入数据库中,而不是JSon文件中的所有记录。

But I want to insert all the records to the database. 但是我想将所有记录插入数据库。

table : tbl_sales
the id field is AutoIncrement

+--------------------+-------------+------+-----+---------+----------------+
| Field              | Type        | Null | Key | Default | Extra          |
+--------------------+-------------+------+-----+---------+----------------+
| sale_item          | varchar(20) | NO   |     | NULL    |                |
| sale_qty           | int(11)     | NO   |     | NULL    |                |
| local_row_added_on | datetime    | NO   |     | NULL    |                |
| last_edited_on     | datetime    | YES  |     | NULL    |                |
| id                 | int(11)     | NO   | PRI | NULL    | auto_increment |
+--------------------+-------------+------+-----+---------+----------------+

<?php
    $connect = mysqli_connect("localhost", "root", "", "mytest"); // 1.
    $query = '';
    $table_data = '';
    $filename = "path.json";

    $data = file_get_contents($filename); // 2.
    $array = json_decode($data, true); // 3. 

    foreach($array as $row) // 4. 
    {
      $query .= "INSERT INTO tbl_sales(sale_item, sale_qty, local_row_added_on, last_edited_on) VALUES ('".$row["sale_item"]."', '".$row["sale_qty"]."', '".$row["local_row_added_on"]."', '".$row["last_edited_on"]."'); ";  // 5. 
      mysqli_query($connect, $query); // 6.         
    }

    echo "<h1>Successfully Imported JSON Data</h1>"; 
    // 1. Connect PHP to MySQL Database
    // 2. Read the JSON file in PHP
    // 3. Convert JSON String into PHP Array
    // 4. Extract the Array Values by using Foreach Loop
    // 5. Make Multiple Insert Query 
    // 6. Run Mutliple Insert Query
?>

JSon file records(path.json) : JSon文件记录(path.json):

[
{
    "sale_item":"Sugar",
    "sale_qty":"5",
    "local_row_added_on":"2018-05-08 10:10:24",
    "last_edited_on":"2018-05-08 10:10:24",
    "id":"1"
},
{
    "sale_item":"Keyboard",
    "sale_qty":"2",
    "local_row_added_on":"2018-05-07 08:14:41",
    "last_edited_on":"2018-05-07 06:14:53",
    "id":"2"
},
{
    "sale_item":"Biscuit",
    "sale_qty":"3",
    "local_row_added_on":"2018-05-06 12:15:17",
    "last_edited_on":"2018-05-06 12:15:35",
    "id":"3"
},
{
    "sale_item":"Pen",
    "sale_qty":"25",
    "local_row_added_on":"2018-05-14 03:20:22",
    "last_edited_on":"2018-05-14 03:20:25",
    "id":"4"
},
{
    "sale_item":"Snacks",
    "sale_qty":"6",
    "local_row_added_on":"2018-05-07 05:30:40",
    "last_edited_on":"2018-05-16 05:30:40",
    "id":"5"}
]

When building your query, you are using 建立查询时,您正在使用

$query .= 

This adds the content onto the previous value. 这会将内容添加到先前的值。 The first time this is OK as there is no previous content. 第一次可以,因为没有先前的内容。 Second time it adds a new insert onto the old one and will fail. 第二次它将新的插入项添加到旧的插入项中,并且将失败。 Remove the . 删除. to just set a new statement. 只是设置一个新的语句。

$query = 

You should also look into prepared statements and bind variables, this helps protect from SQL injection and can aid in other ways. 您还应该查看准备好的语句并绑定变量,这有助于防止SQL注入并以其他方式提供帮助。

Please try this, Your query is : 请尝试这个,您的查询是:

INSERT INTO tbl_sales(sale_item, sale_qty, local_row_added_on, 
last_edited_on) VALUES ('Sugar', '5', '2018-05-08 10:10:24', '2018-05-08 
10:10:24'); 
INSERT INTO tbl_sales(sale_item, sale_qty, local_row_added_on, 
last_edited_on) VALUES ('Keyboard', '2', '2018-05-07 08:14:41', '2018-05-07 
06:14:53'); 
INSERT INTO tbl_sales(sale_item, sale_qty, local_row_added_on, 
last_edited_on) VALUES ('Biscuit', '3', '2018-05-06 12:15:17', '2018-05-06 
12:15:35'); 
INSERT INTO tbl_sales(sale_item, sale_qty, local_row_added_on, 
last_edited_on) VALUES ('Pen', '25', '2018-05-14 03:20:22', '2018-05-14 
03:20:25'); 
INSERT INTO tbl_sales(sale_item, sale_qty, local_row_added_on, 
last_edited_on) VALUES ('Snacks', '6', '2018-05-07 05:30:40', '2018-05-16 
05:30:40'); 

and you should execute your query after the foreach loop: 并且您应该在foreach循环之后执行查询:

$query='';
foreach($array as $row) // 4. 
{
  $query .= "INSERT INTO tbl_sales(sale_item, sale_qty, local_row_added_on, last_edited_on) VALUES ('".$row["sale_item"]."', '".$row["sale_qty"]."', '".$row["local_row_added_on"]."', '".$row["last_edited_on"]."'); ";  // 5. 

}
$result = mysqli_multi_query($connect,$query ) or die("Unable to insert: //6".mysql_error());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM