Search code examples
mysqlcontent-management-systemlast-insert-id

Problems with multiple mySQL inserts


I'm building a simple cms and I'm trying to automatically create an additional in the database when a user signs up. The code worked just fine, with the first insert line, but once I added a second one, it stopped working. I got this code structure from StackOverflow and I've looked on other sites and it seems to be correct, but for the life of me I can't figure out what's wrong. Any suggestions?

$registerquery = mysql_query("BEGIN
INSERT INTO users (Username, Password, EmailAddress) 
VALUES ('".$username."', '".$password."', '".$email."')
INSERT INTO pages (page_title, page_owner) 
VALUES ('My New Page', LAST_INSERT_ID())
COMMIT");

Solution

  • You can't issue two queries in a single query() call with the MySQL driver in PHP. It's a security measure against certain forms of SQL injection attacks - which your code is vulnerable to (unless you're doing mysql_real_escape_string() or similar earlier).

    You're also missing a ; to separate the two queries.

    Multi-query syntax is

    query1; query2; query3; etc...
    

    You've basically got

    query1 query2
    

    which is simply a syntax error.