0

I am looking for a way to write 2 MySQL queries into 1 PHP variable since I am looking to insert into 2 different tables.

I am building a simple registry page where the name is in table "users" and email and password is in table "auth".

Tables as follows:

table users table auth

so I thought something like this:

$sql = "INSERT INTO users VALUES('', '$name','','') INSERT INTO auth VALUES('', '', '$email', $password', '')";

since I am using

$result = mysql_query($sql); 

I would need to have the query in one variable ($sql) as far as I understand.

Thanks.

erezT
  • 186
  • 17
  • Typically you would just do the two inserts separately (wrapping them in a transaction if necessary to ensure integrity.) Also, if you're writing new code, there's [no way you should be using mysql_ functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Matt Gibson Oct 24 '15 at 10:39

2 Answers2

0

If the two tables are related, a straightforward insert will put the data in no problem. Then just get your data into a $result variable.

SELECT username, auth
FROM users
INNER JOIN auth
ON users.id=auth.id;
Hexana
  • 1,095
  • 2
  • 12
  • 35
0

Just add ; to sql query to execute double query

$sql = "INSERT INTO users VALUES('', 'name');INSERT INTO auth VALUES('', '', '$email', $password', '')";

  • When you say "doesn't work" it's always helpful to provide details on exactly the problem you're seeing. – Matt Gibson Oct 24 '15 at 10:37
  • thats not a guess, in mysql command, you can run double query just like that. I have check the php doc about mysql_query(), it says [http://php.net/mysql-query] "mysql_query() sends a unique query **(multiple queries are not supported)** to the currently active database on the server that's associated with the specified." You cant do that with mysql_ extension, you have to use mysqli_ cause mysql_ is deprecated. here the link to use multiple query in mysqli ext: [http://php.net/manual/en/mysqli.quickstart.multiple-statement.php] – Fajry Hamzah Oct 24 '15 at 10:49