我使用c#创建了JSon文件,以从本地MySQL数据库导出所选记录。 php代码用于将JSon文件recods插入MySQL远程数据库。但问题是,当我运行php代码时,它只将JSon中的第一条记录插入数据库而不是JSon文件中的所有记录。
但我想将所有记录都插入数据库。
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文件记录(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"}
]
答案 0 :(得分:2)
构建查询时,您正在使用
.
这会将内容添加到之前的值。这是第一次没关系,因为之前没有内容。第二次它在旧的插件上添加一个新的插件并且会失败。删除$query =
以设置新语句。
// This file is PHP.
<html>
<?php
if ($_POST['submit']) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
$strtadd1 = $_POST['strtadd1'];
$strtadd2 = $_POST['strtadd2'];
$city = $_POST['city'];
$country = $_POST['country'];
$success = '';
// Upon Success.
if ($firstname != '' && $lastname != '' && $email != '' && $password != '' && $confirmpassword != '' && $strtadd1 != '' && $strtadd2 != '' && $city != '' && $country != '') {
// Change $success variable from an empty string.
$success = 'success';
// Insert whatever you want to do upon success.
} else {
// Upon Failure.
echo '<p class="error">Fill in all fields.</p>';
// Set $success variable to an empty string.
$success = '';
}
}
?>
<form method="POST" action="#">
<label class="w">Plan :</label>
<select autofocus="" name="plan" required="required">
<option value="">Select One</option>
<option value="FREE Account">FREE Account</option>
<option value="Premium Account Monthly">Premium Account Monthly</option>
<option value="Premium Account Yearly">Premium Account Yearly</option>
</select>
<br>
<label class="w">First Name :</label><input name="firstname" type="text" placeholder="First Name" required="required" value="<?php if (isset($firstname) && $success == '') {echo $firstname;} ?>"><br>
<label class="w">Last Name :</label><input name="lastname" type="text" placeholder="Last Name" required="required" value="<?php if (isset($lastname) && $success == '') {echo $lastname;} ?>"><br>
<label class="w">E-mail ID :</label><input name="email" type="email" placeholder="Enter Email" required="required" value="<?php if (isset($email) && $success == '') {echo $email;} ?>"><br>
<label class="w">Password :</label><input name="password" type="password" placeholder="********" required="required" value="<?php if (isset($password) && $success == '') {echo $password;} ?>"><br>
<label class="w">Re-Enter Password :</label><input name="confirmpassword" type="password" placeholder="********" required="required" value="<?php if (isset($confirmpassword) && $success == '') {echo $confirmpassword;} ?>"><br>
<label class="w">Street Address 1 :</label><input name="strtadd1" type="text" placeholder="street address first" required="required" value="<?php if (isset($strtadd1) && $success == '') {echo $strtadd1;} ?>"><br>
<label class="w">Street Address 2 :</label><input name="strtadd2" type="text" placeholder="street address second" value="<?php if (isset($strtadd2) && $success == '') {echo $strtadd2;} ?>"><br>
<label class="w">City :</label><input name="city" type="text" placeholder="City" required="required" value="<?php if (isset($city) && $success == '') {echo $city;} ?>"><br>
<label class="w">Country :</label><select autofocus="" id="a1_txtBox1" name="country" required="required" placeholder="select one" value="<?php if (isset($country) && $success == '') {echo $country;} ?>">
<input type="submit" name="submit">
</form>
</html>
您还应该查看预处理语句和绑定变量,这有助于防止SQL注入,并可以通过其他方式提供帮助。
答案 1 :(得分:1)
请试试这个,您的查询是:
$sql = "SELECT * FROM main SET PendDays = CURRENT_DATE - RequiredDate ORDER BY ReqstDate DESC"
你应该在foreach循环之后执行你的查询:
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');