[英]Exporting a Line of Data From One Data Table to Another
The code below is for a project I'm working on.下面的代码适用于我正在处理的项目。 I'm having some issues with my PHP code and am in need of help.
我的 PHP 代码有一些问题,需要帮助。
I have a button on my data table named "Export".我的数据表上有一个名为“导出”的按钮。 When the button is clicked, I wish to copy the data on that row and move it to an archive.
单击该按钮后,我希望复制该行上的数据并将其移动到存档中。
<?php
function val($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "databasename";
$ticket_id = $_GET["ticket_id"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM activeticket WHERE ticket_id='$ticket_id' INSERT INTO `ticketarchive`(`name`, `account_num`, `department`, `ticket_desc`, `email`, `assigned`, `status`, `fibre_site`) VALUES ([name],[account_num],[department],[ticket_desc],[email],[assigned],[status],[fibre_site])";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully. Record ID is: ";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Below is the error that this produces:以下是由此产生的错误:
Error: SELECT * FROM activeticket WHERE ticket_id='1' INSERT INTO
archiveticket
(name
,account_num
,department
,ticket_desc
,assigned
,status
,fibre_site
) VALUES ([name],[account_num],[department],[ticket_desc],[email],[assigned],[status],[fibre_site]) You have an error in your SQL syntax;错误:SELECT * FROM activeticket WHERE ticket_id='1' INSERT INTO
archiveticket
(name
,account_num
,department
,ticket_desc
,assigned
,status
,fibre_site
) VALUES ([name],[account_num],[department],[ticket_desc], [email],[assigned],[status],[fiber_site]) 你的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTOarchiveticket
(name
,account_num
,department
,ticket_desc
, `' at line 1检查与您的 MariaDB 服务器版本相对应的手册,以了解在“INSERT INTO
archiveticket
(name
、account_num
、department
、ticket_desc
、第 1 行的‘’)附近使用的正确语法
First execute the select query and after that execute the insert one.首先执行选择查询,然后执行插入查询。 Right now you are trying to run them both and this is the problem.
现在您正试图同时运行它们,这就是问题所在。
This isn't really a PHP issue you're having, you just seem to be unfamiliar with SQL.这实际上并不是您遇到的 PHP 问题,只是您似乎不熟悉 SQL。 What you're trying to do is insert the result of a SELECT query into a table.
您要做的是将 SELECT 查询的结果插入到表中。 This isn't the way to do it at all.
这根本不是这样做的方法。 What you're looking for is :
你要找的是:
$sql = "INSERT INTO `ticketarchive`(
`name`,
`account_num`,
`department`,
`ticket_desc`,
`email`,
`assigned`,
`status`,
`fibre_site`
)
SELECT
`name`,
`account_num`,
`department`,
`ticket_desc`,
`email`,
`assigned`,
`status`,
`fibre_site`
FROM
`activeticket`
WHERE
`ticket_id` = $ticket_id"
For more information, read here .有关更多信息,请阅读此处。
I'd also advice you look into parametrized queries to avoid SQL injections.我还建议您研究参数化查询以避免 SQL 注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.