0

i am trying to execute multiple query's at once / or one after the other but it's failing. why is this, here's how i've structured my code:

$query="
INSERT INTO ptb_users (user_id,
id,
first_name,
last_name,
email )
VALUES('NULL',
'NULL',
'".$firstname."',
'".$lastname."',
'".$email."'
)";
mysql_query($query) or die();
$result = mysql_query("UPDATE ptb_users SET user_id=id");

$query="INSERT INTO ptb_profiles (id,
user_id,
display_name )
VALUES('NULL',
'NULL',
'".$username."'
)";
mysql_query($query) or die();
$result = mysql_query("UPDATE ptb_profiles SET id=user_id");
James Cart
  • 11
  • 2

3 Answers3

0

You should really use PDO and prepared statements, mysql_* is no longer supported. It will make your life easier especially for repeating statements.

Otherwise, what is your error?

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Brock Hensley
  • 3,617
  • 2
  • 29
  • 47
  • Even if your answer really does not answer his question - it is sound advice not to use an obsolete interface. – fredrik Mar 15 '13 at 12:31
0

Use different name for variable $query and $result

it should be

$query="INSERT INTO ptb_users (user_id,id,first_name,last_name,email )VALUES('NULL','NULL','".$firstname."','".$lastname."','".$email."')";
$result = mysql_query($query) or die();
$result1 = mysql_query("UPDATE ptb_users SET user_id=id");

$query2="INSERT INTO ptb_profiles (id,user_id,display_name )VALUES('NULL','NULL','".$username."')";
$result2 = mysql_query($query2) or die();
$result3 = mysql_query("UPDATE ptb_profiles SET id=user_id");
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
  • That should not affect if the queries succeed or not - only wether he retains any usable information about previous queries. – fredrik Mar 15 '13 at 12:30
0

You are trying to insert 'NULL' in fields which I'm guessing are auto_increment columns. You should omit them from the query:

$query = "INSERT INTO ptb_users (first_name, last_name, email )
            VALUES('".$firstname."', '".$lastname."', '".$email."')";

Even if the null should be there, according to your implementation/definition of the tables, they should not be enclosed in ' as that means the string NULL, not the value.

fredrik
  • 6,483
  • 3
  • 35
  • 45