0
$query = mysql_query( "insert into users (id,email,password,firstname,lastname) values ('','$email','$password','$firstname','$lastname')" ) or die(mysql_error()); echo $userid = mysql_insert_id(); $q_query = mysql_query( "insert into qualification (q_id) values ('$userid')" ) or die(mysql_error()); 

echo $userid shows perfectly but the row is not inserted into the qualification table. Why not?

11
  • 1
    Are you sure that first query is even working? You're trying to insert an id of '' into the users table; that makes no sense to me. Edit: obviously it is, or else the echo wouldn't show right. But why it would work I still don't know ( see below); Commented Jan 2, 2015 at 22:36
  • 1
    Does or die(mysql_error()) give you a error message? Commented Jan 2, 2015 at 22:36
  • 2
    @DanFarrell Assuming auto_increment for id, an empty string '' or null will both just cause it to use the next auto-inc as normal. (personally I prefer to leave it out of the list though) Commented Jan 2, 2015 at 22:37
  • 1
    Is q_id an integer? if so, change ('$userid') to ($userid) Commented Jan 2, 2015 at 22:38
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi. Commented Jan 2, 2015 at 22:39

1 Answer 1

1

I suspect @Gil has the right answer. Using the mysql function last_insert_id() is a more straightforward approach however:

mysql> create temporary table users (id int unsigned not null auto_increment primary key, email text, password text, firstname text, lastname text); Query OK, 0 rows affected (0.00 sec) mysql> insert into users (id,email,password,firstname,lastname) values ('','[email protected]', 'password','dan','f'); ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1 

This depends on whether you're in strict mode (thanks to comment below )

mysql> insert into users (id,email,password,firstname,lastname) values (null,'[email protected]', 'password','dan','f'); Query OK, 1 row affected (0.00 sec) mysql> create table qualification (q_id int unsigned not null); Query OK, 0 rows affected (0.02 sec) mysql> insert into qualification( q_id) values (last_insert_id() ); Query OK, 1 row affected (0.00 sec) mysql> select * from users,qualification; +----+--------------+----------+-----------+----------+------+ | id | email | password | firstname | lastname | q_id | +----+--------------+----------+-----------+----------+------+ | 1 | [email protected] | password | dan | f | 1 | +----+--------------+----------+-----------+----------+------+ 1 row in set (0.00 sec) 
Sign up to request clarification or add additional context in comments.

1 Comment

"I don't understand why it's working for you": It depends on strict SQL mode (bugs.mysql.com/bug.php?id=61375)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.