用于插入然后删除数据的多查询
Multi Query for inserting then deleting data
我很难理解多查询的工作原理。
我想将一个 table 中的数据插入另一个 table 中,然后删除原始 table 中的数据。我怎样才能做到这一点?下面的方法可行吗?
<?php
$con = mysqli_connect('localhost:3308','root','');
if(!$con)
{
echo 'Not Connected To Server';
}
if(!mysqli_select_db($con, 'Database'))
{
echo 'Database Not Selected';
}
$jobNumber = $_POST['jobNumberInsert'];
$siteName = $_POST['siteNameInsert'];
$phoneNumber = $_POST['phoneNoInsert'];
$firstName = $_POST['firstNameInsert'];
$lastName = $_POST['lastNameInsert'];
$streetAddress = $_POST['streetAddressInsert'];
$linetwoAddress = $_POST['linetwoAddressInsert'];
$city = $_POST['cityInsert'];
$county = $_POST['countyInsert'];
$postcode = $_POST['postcodeInsert'];
$serviceInfo = $_POST['serviceInfoInsert'];
$jobDate = $_POST['jobDateInsert'];
$priority_value = $_POST['priorityInsert'];
$sql = "INSERT INTO table2(jobNumber,siteName,phoneNumber,firstName,lastName,streetAddress,linetwoAddress,city,county,postcode,serviceInfo,jobDate,priority)
VALUES ('$jobNumber','$siteName','$phoneNumber','$firstName','$lastName','$streetAddress','$linetwoAddress','$city','$county','$postcode','$serviceInfo','$jobDate','$priority_value')";
$sql .= "DELETE FROM table1 WHERE jobNumber= $jobNumber";
if(!mysqli_multi_query($con,$sql))
{
echo 'Not Inserted or Deleted';
}
else
{
echo 'Inserted and Deleted';
}
header("refresh:2 url=index.php");
?>
目前在执行代码时没有任何反应。当单独执行这些语句时,它们就会起作用。
没有; (分号)在第一个语句之后。
$sql = "INSERT INTO table2(jobNumber,siteName,phoneNumber,firstName,lastName,streetAddress,linetwoAddress,city,county,postcode,serviceInfo,jobDate,priority) VALUES ('$jobNumber','$siteName','$phoneNumber','$firstName','$lastName','$streetAddress','$linetwoAddress','$city','$county','$postcode','$serviceInfo','$jobDate','$priority_value');";
所以,它试图作为单个语句而不是单独的语句执行。
警告:您对SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough!
持开放态度
不要使用 mysqli_multi_query()
!
您只有 2 个查询,无论如何都是分开的。无需将它们一起发送到 MySQL.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = new mysqli('localhost:3308','root','', 'Database');
$con->set_charset('utf8mb4');
$stmt = $con->prepare('INSERT INTO table2(jobNumber,siteName,phoneNumber,firstName,lastName,streetAddress,linetwoAddress,city,county,postcode,serviceInfo,jobDate,priority)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)');
$stmt->bind_param('sssssssssssss', $_POST['jobNumberInsert'],
$_POST['siteNameInsert'],
$_POST['phoneNoInsert'],
$_POST['firstNameInsert']
...
);
$stmt->execute();
$stmtDel = $con->prepare('DELETE FROM table1 WHERE jobNumber=?');
$stmt->bind_param('s', $_POST['jobNumberInsert']);
$stmt->execute();
header("refresh:2 url=index.php");
?>
我也觉得你应该把它包装在一个事务中。如果删除失败,那么您可能希望插入也被回滚。
我很难理解多查询的工作原理。
我想将一个 table 中的数据插入另一个 table 中,然后删除原始 table 中的数据。我怎样才能做到这一点?下面的方法可行吗?
<?php
$con = mysqli_connect('localhost:3308','root','');
if(!$con)
{
echo 'Not Connected To Server';
}
if(!mysqli_select_db($con, 'Database'))
{
echo 'Database Not Selected';
}
$jobNumber = $_POST['jobNumberInsert'];
$siteName = $_POST['siteNameInsert'];
$phoneNumber = $_POST['phoneNoInsert'];
$firstName = $_POST['firstNameInsert'];
$lastName = $_POST['lastNameInsert'];
$streetAddress = $_POST['streetAddressInsert'];
$linetwoAddress = $_POST['linetwoAddressInsert'];
$city = $_POST['cityInsert'];
$county = $_POST['countyInsert'];
$postcode = $_POST['postcodeInsert'];
$serviceInfo = $_POST['serviceInfoInsert'];
$jobDate = $_POST['jobDateInsert'];
$priority_value = $_POST['priorityInsert'];
$sql = "INSERT INTO table2(jobNumber,siteName,phoneNumber,firstName,lastName,streetAddress,linetwoAddress,city,county,postcode,serviceInfo,jobDate,priority)
VALUES ('$jobNumber','$siteName','$phoneNumber','$firstName','$lastName','$streetAddress','$linetwoAddress','$city','$county','$postcode','$serviceInfo','$jobDate','$priority_value')";
$sql .= "DELETE FROM table1 WHERE jobNumber= $jobNumber";
if(!mysqli_multi_query($con,$sql))
{
echo 'Not Inserted or Deleted';
}
else
{
echo 'Inserted and Deleted';
}
header("refresh:2 url=index.php");
?>
目前在执行代码时没有任何反应。当单独执行这些语句时,它们就会起作用。
没有; (分号)在第一个语句之后。
$sql = "INSERT INTO table2(jobNumber,siteName,phoneNumber,firstName,lastName,streetAddress,linetwoAddress,city,county,postcode,serviceInfo,jobDate,priority) VALUES ('$jobNumber','$siteName','$phoneNumber','$firstName','$lastName','$streetAddress','$linetwoAddress','$city','$county','$postcode','$serviceInfo','$jobDate','$priority_value');";
所以,它试图作为单个语句而不是单独的语句执行。
警告:您对SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough!
持开放态度不要使用 mysqli_multi_query()
!
您只有 2 个查询,无论如何都是分开的。无需将它们一起发送到 MySQL.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = new mysqli('localhost:3308','root','', 'Database');
$con->set_charset('utf8mb4');
$stmt = $con->prepare('INSERT INTO table2(jobNumber,siteName,phoneNumber,firstName,lastName,streetAddress,linetwoAddress,city,county,postcode,serviceInfo,jobDate,priority)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)');
$stmt->bind_param('sssssssssssss', $_POST['jobNumberInsert'],
$_POST['siteNameInsert'],
$_POST['phoneNoInsert'],
$_POST['firstNameInsert']
...
);
$stmt->execute();
$stmtDel = $con->prepare('DELETE FROM table1 WHERE jobNumber=?');
$stmt->bind_param('s', $_POST['jobNumberInsert']);
$stmt->execute();
header("refresh:2 url=index.php");
?>
我也觉得你应该把它包装在一个事务中。如果删除失败,那么您可能希望插入也被回滚。