0

I would like execute a query when a mysql record is deleted in order to re-order ID autoincrement.

// delete the record :

if(isset($_REQUEST['id']) && $_REQUEST['id']<>'')
  {
      $delete=mysql_query("delete from planning where id='".$_REQUEST['id']."'");

     echo "<script type=\"text/javascript\">".
        "alert('delete ok');".
        "</script>";
  }
  $msg='';
  if(isset($_REQUEST['msg']))
  {
      $msg=$_REQUEST['msg'];
  }

if(isset($_REQUEST['delete']) && $_REQUEST['delete']<>'' && $_REQUEST['delete'] == 'true')
  {
      $planning_id = $_REQUEST['id'];

      $query = "delete from planning where id = '".$planning_id."' " ; ;
      $output=mysql_query($query);
  }

And the mysql query that I can't execute but working with phpmyadmin :

ALTER TABLE `planning` DROP `id`;
ALTER TABLE `planning` AUTO_INCREMENT = 1;
ALTER TABLE `planning` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

Anyone to an idea please ?

Warmest thanks by advance !

2
  • For what reason do the ID's need to me re-ordered? Commented Nov 12, 2015 at 9:03
  • never pass user input to your directly SQL, try to use prepared statements. Can you post your error output from mysql, maybe it's a permission error Commented Nov 12, 2015 at 9:03

1 Answer 1

1

Don't do that. It will add significant overhead to your database operations if you keep dropping the automatically incremented primary key and then reassign it especially once your dataset gets large enough.

Consider changing your primary key system.

Also, your code is vulnerable to SQL injection. Switch to prepared statements The mysql_ set of function calls is deprecated as well so even more reason to switch.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.