简体   繁体   English

用于插入然后删除数据的多查询

[英]Multi Query for inserting then deleting data

I am struggling to understand how multi queries work.我很难理解多查询是如何工作的。

I want to insert data from one table into another, then delete the data from the original table.我想将一个表中的数据插入到另一个表中,然后从原始表中删除数据。 How can I do this?我怎样才能做到这一点? Is the method below viable?下面的方法可行吗?

<?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");
?>

Currently upon executing the code nothing happens.目前在执行代码时什么也没有发生。 When the statements are executed individually, then they work.当语句单独执行时,它们就会起作用。

Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries.警告:您对SQL 注入持开放态度,应该使用参数化的准备好的语句,而不是手动构建查询。 They are provided by PDO or by MySQLi .它们由PDOMySQLi 提供 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!逃避还不够!

Do not use mysqli_multi_query() !不要使用mysqli_multi_query()

You only have 2 queries, which are separate anyway.您只有 2 个查询,无论如何它们都是分开的。 There's no need to send them together to MySQL.没有必要将它们一起发送到 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");
?>

I also feel like you should wrap this in a transaction.我也觉得你应该把它包装在一个交易中。 If the delete fails, then it's likely you would like the insert to be rolled back too.如果删除失败,那么您很可能希望插入也回滚。

There is no ;没有; (semicolon) after first statement. (分号)在第一条语句之后。

$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');";

So, it's trying to execute as single statement instead of separate statement.因此,它试图作为单个语句而不是单独的语句执行。

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

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