简体   繁体   中英

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.

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. Otherwise the SQL syntax works fine.

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 . But only the first mysql_query gets executed how do i make it so the second one gets executed also ?

And another question is is it possible to get <a href="munka/index.php?x=5" > [-] </a> the x=5 with a $_POST ?

使用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. Check the php error log.

for the second question $_GET is used to take parameters from the URL for example

munka/index.php?x=5

$_POST is used to get parameters posted on http post (usually on form submits).

只需将更新查询更改为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. It is work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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