0

I have a table that contains

ID(P)(auto)  NAME
----------------------- 
1            Art
4            Bankie
6            Cara

The data sequence has 3 missing ID values: 2, 3 and 5.

This is my query:

$fname = $_POST['fname'];
mysql_query("INSERT INTO applicants ($fname) VALUES ('$fname')");

How can data be INSERTed in place of a missing ID value, in order to use all ID's.

Brandon White
  • 997
  • 9
  • 22
kim de castro
  • 299
  • 6
  • 19

2 Answers2

1

What you're asking is not necessary for performance purposes, however to my knowledge there isn't a way to do this automatically. You would have to check for the missing ID ranges and assign them manually.

I would also recommend looking at the following post about SQL injection protection as it's a necessary step to maintaining a secure database connection. SQL Injection Prevention

Secondly, please do not use php's mysql_query() functions. These are deprecated and have been for some time now. MySQLi or PDO is recommended instead.

Community
  • 1
  • 1
Brandon White
  • 997
  • 9
  • 22
0

It's could be done using variable in sql query.

Let's say $name = $_POST['fname'];

Then you need insert query like this :

insert into yourtable(id,name)
select id-diff as new_id,'$name' from(
    select id,id - (@x := @x + 1) as diff
    from yourtable
    join (select @x := 0) as t1
    order by id asc
) as t2
where diff <> 0
limit 1
Hotdin Gurning
  • 1,821
  • 3
  • 15
  • 24