嘿伙计们,我想问如何在 mysql 数据库中插入多行,尽管事实上我已经知道如何在 mysql 数据库中插入一行,但我不知道如何插入多行。
这是确切的场景
当我填写日期和备注时,我单击“添加”按钮,它应该插入到我的数据库中。
这是我的 php 脚本,我知道它有问题。
<?php
require("config.inc.php");
if(!empty($_POST)){
if(empty($_POST['date']) || empty($_POST['remark'])){
$response["Success"] = 0;
$response["Message"] = "Please complete all fields.";
die(json_encode($response));
}
$query = "SELECT 1 FROM tb_attendance WHERE date = :date";
$query_params = array(
':date'=> $_POST['date'],
);
try{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){
$response["Success"] = 0;
$response["Message"] = "Database Error. Please Try Again...";
die(json_encode($response));
}
$row = $stmt->fetch();
if($row){
$response["Success"] = 0;
$response["Message"] = "I'm sorry, this Record is already existed.";
die(json_encode($response));
}
$query = "INSERT INTO tb_attendance (date, remark) VALUES(:date, :remark)";
$query_params = array(
':date' => $_POST['date'],
':remark' => $_POST['remark']
);
try{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){
$response["Success"] = 0;
$response["Message"] = "Database Error1. Please Try Again...";
die(json_encode($response));
}
$response["Success"] = 1;
$response["Message"] = "Attendance Successfully Recorded.";
echo json_encode($response);
}else{
?>
<form action="addAttendance.php" method="post">
Date: <input type="text" name="date"><br>
Remark: <input type="text" name="remark"><br>
<input type="submit">
</form>
<?php
}
?>
最佳答案
只需在数组中发送发布数据,然后使用该数组创建一个用于插入大量记录的查询
关于php - 如何在mysql数据库中插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882056/