1

I have a 'Users' table in my database and when someone makes a new account I want to, instead of doing an auto increment for the id, get the value of the last id inserted, add 1 to it, then insert it with the rest of the create user data. My existing code is:

<?php
mysql_connect("localhost","username","password");
mysql_select_db("db");

$fname = $_POST['fname'];
// ... get all the other info from create user form


$query = "INSERT INTO Users ('fname','lname','email','username','password') 
          VALUES 
          ('$fname','$lname','$email','$username','password')"

What I would want to do is get the 'id' from the Users table of the last user (row) in the table, then add 1 to it, and have the code be this:

$query = "INSERT INTO Users ('id','fname','lname','email','username','password') 
VALUES 
('$id','$fname','$lname','$email','$username','$password')";

How would I go about doing this?

EDIT: Thank you all for caring about me enough to remind me of SQL Injection. The code I provided is a minimal example of what I am doing. I didn't want to put all of it into my example. But thanks anyway

wxrunning
  • 41
  • 6
  • Watch out SQL Injection. You should escape the input from POST before inserting into SQL – Raptor Jan 22 '15 at 03:28
  • why are you using phps mysql api? its deprecated... if you use mysqli or pdo you can use the last_insert_id that is supported in their api's – John Ruddell Jan 22 '15 at 03:29
  • Yes I know @Raptor. The code I provided isn't exact. It is just a rough sketch of what I have it doing. thanks for the input though – wxrunning Jan 22 '15 at 03:31
  • What is the use of `+1`d value? It sounds wrong – zerkms Jan 22 '15 at 03:34

2 Answers2

3
  1. mysql_insert_id()

    mysql_query("INSERT QUERY ...");
    $id = mysql_insert_id();
    
  2. Use mysqli instead (or PDO).

    $mysqli->query($query);
    $id = $mysqli->insert_id
    printf ("New Record has id %d.\n", $id);
    
  3. Escape properly! Because you are vulnerable to SQL injection.

  4. increment: $lastid = $lastid + 1; or just $id++.

    The auto-increment is there for a reason. Use it.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

You can use mysql_insert_id(); function:

$query = "INSERT INTO Users ('fname','lname','email','username','password') 
          VALUES 
          ('$fname','$lname','$email','$username','password')"
$lastid=mysql_insert_id(); /* GET THE ID OF THE INSERT QUERY */
$lastid++; /* INCREMENT THE ID */

Some tips for your future: Use mysqli prepared statement instead of mysql. Read more about SQL injections here.

Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • `$newincrementedid` --- what's that? – zerkms Jan 22 '15 at 03:32
  • @zerkms - You said you wanted to get the last ID, and then increment it by yourself?! – Logan Wayne Jan 22 '15 at 03:34
  • @zerkms - Ooops.. :P – Logan Wayne Jan 22 '15 at 03:36
  • As of `+1` - I think it worth making a note that `incremented value + 1` makes no sense. It's like a meaningless info that OP should not use in any way at first place. I agree that you just answered what OP asked, but we're here try to prevent people from doing stupid things. – zerkms Jan 22 '15 at 03:36
  • For the downvoter, don't be bitter just because the OP accepted my answer than the other answer that you prefer. Not my fault. Thanks :) – Logan Wayne Jan 22 '15 at 03:44
  • I didn't downvote, but `$lastid=$lastid++;` <--- what's that o_O – zerkms Jan 22 '15 at 03:45
  • Are you sure it shouldn't be `++$lastid`? (it's funny that both answers were affected by the same mistake) – zerkms Jan 22 '15 at 03:46
  • @LoganWayne I downvoted and i didn't answer... not being bitter and honestly thats a little childish for you to think that.... the reason for the downvote is because you have code in the answer that is not only unnecessary its wrong... your increment is wrong and if its used for another query it will be an incorrect value – John Ruddell Jan 22 '15 at 03:47
  • @LoganWayne if you want to have another variable called $lastid then you need to do `$lastid = $lastid + 1;` – John Ruddell Jan 22 '15 at 03:49
  • @JohnRuddell I cannot believe `+ 1` is the hardest part of the question :-D – zerkms Jan 22 '15 at 03:49
  • @zerkms Right??? basic stuff here lol – John Ruddell Jan 22 '15 at 03:50
  • 2
    `$lastid=++$lastid` --- OH GOD. Seriously, how hard is it to add `1` to an integer? – zerkms Jan 22 '15 at 03:50
  • @zerkms #redundantcode – John Ruddell Jan 22 '15 at 03:51
  • Guys, my first answer was just a simple `$newid=$lastid+1;`, then @zerkms didn't like it. So I changed and made it worse. Don't worry. I'll delete my answer and hang myself. Thanks – Logan Wayne Jan 22 '15 at 03:56
  • 2
    @LoganWayne i don't know why a downvote matters that much to you... I removed my downvote because you now have a correct answer as opposed to an incorrect answer. even though it was changed from our responses... its just a website. dont freakout about a simple downvote, its a part of life... sometimes theres negatives and sometimes positives its a part of life. We all learn from it and grow – John Ruddell Jan 22 '15 at 04:02
  • "then @zerkms didn't like it" --- I'm sorry? I didn't "didn't like" your implementation in particular, I didn't like the whole idea of modifying an autoincrement value overall – zerkms Jan 22 '15 at 04:11