1

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 ?

tereško
  • 58,060
  • 25
  • 98
  • 150
Csak Zoli
  • 408
  • 1
  • 4
  • 11
  • 7
    Are you sure you want to to that? And are you sure you really have to to this? At the first look it doesen't seem to be a good idea to change IDs of already existing entries. Just imagine you have more tables that use the ID of a client to reference some data. You would have to update all the IDs there too. Do you really want to do such things? – oktopus Feb 21 '13 at 11:37
  • Why do you want to do this? If ID the primary key? And is it auto increment? – I Perfect Feb 21 '13 at 11:37
  • 6
    Jesus .... your code us vulnerable to sql injection and you are using obsolete api ... use either [PDO or IMO only PDO check this ](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189) and also check this (to know about sql injection)[http://stackoverflow.com/questions/134099/are-pdo-prepared-statements-sufficient-to-prevent-sql-injection/12202218#12202218) – NullPoiиteя Feb 21 '13 at 11:39
  • For your second question see the query_string variable described here: http://php.net/manual/en/reserved.variables.server.php – starshine531 Feb 21 '13 at 11:41
  • If you are using auto_increment for the id-field your code won't run after you have deleted one client once. What happens? Right know the ID is the same as the number of clients. But if you have deleted one client the ID of the next client will be 51 but the number of clients will still be 50. So `$last` will have the wrong value for your further mysql_querys – oktopus Feb 21 '13 at 11:42
  • Please read this: http://en.wikipedia.org/wiki/SQL_injection – starshine531 Feb 21 '13 at 11:42
  • You should look into sanitizing your $_GET['x'] variable as using it directly in a mysql query leaves it open to mysql injection. For the $_POST question you need to submit it via an html form. You could use a hidden input to hold the value of the id. If you don't want to have to click a submit button for the form, you can look into using javascript to submit the form on a page load. Also on the second query you need a space before the where. –  Feb 21 '13 at 11:43
  • This is the worst approach to understanding what a db is. First of all you are modifying a key, and second you are deleting it, instead mark as deleted with some "expiration" field. Why do you use ids if you will change them? Also use mysqli, mysql is deprecated. – Leandro Bardelli Feb 21 '13 at 11:44
  • also if $_GET['x'] have white space it will be treated as set . – NullPoiиteя Feb 21 '13 at 11:46
  • Why are you using `MySQL_*` functions? They'll be deprecated in 5.5 - use `MySQLi_*` or `PDO_*` functions instead. If you **must** use this, at least use `mysql_real_escape_string` on your input, dayum! – Jimbo Feb 21 '13 at 11:57

5 Answers5

0

You might save yourself a lot of trouble by using mysql's replace query: see http://dev.mysql.com/doc/refman/5.6/en/replace.html for details.

starshine531
  • 601
  • 5
  • 19
0

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).

udnisap
  • 899
  • 1
  • 10
  • 19
0

just change the update query with a space before the where clause

 mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].' WHERE ID='.(int)$last);
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Nitin Bohra
  • 126
  • 5
0

Better to use transactions support by using InnoDB Mysql DB Engine, so both delete and update execute together wuth COMMIT without miss , and in case anything goes wrong your delete changes get ROLLBACK

Himanshu Patel
  • 172
  • 2
  • 8
-2
 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.