简体   繁体   English

如何在MySQL中同时删除和更新数据

[英]How to delete and update data in MySQL at the same time

Basically what I am trying to do is when I delete a client with an ID of lets say 6 and I have 50 clients, I then want to update the client with the ID of 50 to 6. 基本上,我想做的是删除ID为6的客户端,并且有50个客户端,然后将ID为50的客户端更新为6。

This is my code, in PHP, but it won't execute 2 mysql_query -s at the same time at least I think that's the problem. 这是我的代码,在PHP中,但至少不会同时执行2 mysql_query -s,至少我认为这是问题所在。 Otherwise the SQL syntax works fine. 否则,SQL语法可以正常工作。

public function delete () {
    $last=$this->numrow; //contains last ID works fine

    if (isset ($_GET['x'])) {
        mysql_query('DELETE FROM proba WHERE ID ='.$_GET['x']);
        mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].'WHERE ID='.(int)$last);
    }
}

The $_GET['x'] contains the ID on which it was clicked . $_GET['x']包含单击它的ID。 But only the first mysql_query gets executed how do i make it so the second one gets executed also ? 但是只有第一个mysql_query被执行,我该如何做,所以第二个也要执行?

And another question is is it possible to get <a href="munka/index.php?x=5" > [-] </a> the x=5 with a $_POST ? 另一个问题是是否可以通过$_POST获得<a href="munka/index.php?x=5" > [-] </a> x = 5?

使用mysql的replace查询可能会为您省去很多麻烦:有关详细信息,请参见http://dev.mysql.com/doc/refman/5.6/en/replace.html

most probably you are facing a php error on the first query. 最有可能您在第一个查询中遇到php错误。 Check the php error log. 检查php错误日志。

for the second question $_GET is used to take parameters from the URL for example 对于第二个问题,例如, $_GET用于从URL获取参数

munka/index.php?x=5

$_POST is used to get parameters posted on http post (usually on form submits). $_POST用于获取在http帖子(通常在表单提交)上发布的参数。

只需将更新查询更改为where子句前的空格

 mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].' WHERE ID='.(int)$last);

最好通过使用InnoDB Mysql DB Engine来使用事务支持,因此删除和更新将在COMMIT一起执行而不会遗漏,并且万一出问题,您的删除更改将得到ROLLBACK

 if (isset($_GET['x'])) {
        mysql_query('DELETE FROM proba WHERE ID =' . $_GET['x']);
        mysql_query('ALTER TABLE `proba`  DROP `ID`;');
        mysql_query('ALTER TABLE  `proba` ADD  `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST');
    }

Try in Phpmyadmin delete record no of 5 and drop id column and recreate id column. 尝试在Phpmyadmin中删除5号记录,然后删除id列并重新创建id列。 It is work. 是工作

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

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