-2

我的 php 页面上有删除选项,但我添加了在删除之前存档数据的选项,但同时我想用个人会话用户名更新用户名。

$id=$_GET['id'];

 $sql="INSERT INTO c_archive_table(tech, eng, dr) SELECT tech, eng, dr FROM   `Com` WHERE `id`='$id'";
 $sql_user = "INSERT INTO  c_archive_table('','user','$_SESSION['username']') WHERE `id`='$id'";
 $sql_delete="DELETE FROM `Com` WHERE `id`='$id'";

所以转移到存档和删除工作但将用户会话添加到用户列不起作用。

4

2 回答 2

2

它不会是一个INSERT它将是一个UPDATE

 $sql_user = "UPDATE c_archive_table SET 'user'='".$_SESSION['username']."' WHERE `id`='$id'";

用名称进入user的任何列名替换列$_SESSION

于 2013-07-09T20:39:31.223 回答
1
$sql = new mysqli(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);

// 1. Find the row in the existing table and get the contents
$query1 = '
SELECT
   `tech`,
   `eng`,
   `dr`
FROM `Cnom`
WHERE
   `id` = "'.$sql->real_escape_string($_GET['id']).'"
;';
   // Use real_escape_string to sanitize anything that the user could modify
$result1 = $sql->query($query1) or die ("<pre>Query failed:\n$query1</pre>");
   // die()ing with the query is always helpful for debugging
$row1 = $result1->fetch_assoc() or die ("<pre>No result returned for id {$_GET['id']}</pre>");

// 2. Insert the contents into the archive
$query2 = '
INSERT INTO `c_archive_table` (
   `user`,
   `tech`,
   `eng`,
   `dr`
)
VALUES (
   "'.$sql->real_escape_string($_SESSION['username']).'",
   "'.$sql->real_escape_string($row1['tech']).'",
   "'.$sql->real_escape_string($row1['eng']).'",
   "'.$sql->real_escape_string($row1['dr']).'"
);';
$sql->query($query2) or die ("<pre>Query failed:\n$query2</pre>");

// 3. Delete from the original table
$query3 = '
DELETE FROM `Cnom`
WHERE
   `id` = "'.$sql->real_escape_string($_GET['id']).'"
;';
$sql->query($query3) or die ("<pre>Query failed:\n$query3</pre>");

根据我猜你的数据库表的样子,这可能是一个好的开始。

顺便说一句,在诊断 MySQL 问题时,我建议您在此示例中这样做:在多行中使用缩进编写查询;并使用die (string)PHP 的构造来打印产生错误的查询。然后,您可以清楚地查看查询以查看任何明显的语法错误,并且 MySQL 还可以告诉您错误发生在哪一行。您还可以将die()d 查询复制并粘贴到 phpMyAdmin 中。


更重要的是,这可能不是正确的设置。您应该拥有的,而不是带有几乎相同信息的两个冗余表,是一个带有 column 的表archived。然后您只需更改archived为布尔值 (true) 并在您尝试访问它时检查它。

例如(伪代码):

if (accessing_all_records) {
   // Access all records that aren't archived
   $query = '
SELECT
   *
FROM `Cnom`
WHERE
   `archived` = 0
;';
}

if (inserting_new_record) {
   // Create a new record and set archived to 0 by default (better yet, give it a default value)
   $query = '
INSERT INTO `Cnom` (
   `field_1`,
   ...,
   `archived`
)
VALUES (
   value_1,
   ...,
   0
);';
}

if (archiving) {
   // Update the record and set the archived value to 1
   $query = '
UPDATE `Cnom`
SET
   `archived` = 1
WHERE
   `id` = id
;';
}
于 2013-07-09T20:42:43.390 回答