How can I insert data into two different tables?

Asked

Viewed 70 times

1

I created a function to insert information to two different tables, but it is showing an error:

You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near 'Insert into client(name, reg, Adress, phone) values('mary',2147483647,'a' at line 2

My job is:

function newUser($connect, $name, $reg, $address, $phone, $email, $pass){
    $query = "insert into user(email, password) values('{$email}','{$pass}'); 
              insert into client(name, reg, address, phone) values('{$name}','{$reg}','{$address}','{$phone}')";
    return mysqli_query($connect, $query);
}

When I test the direct query in the bank, it works. :(

  • 2

    Rafael, as the name suggests (Stackoverflow in Portuguese), the official language used here is Portuguese. So could you please translate your question? If you prefer, you can also ask the same question on Stackoverflow website in English.

  • This looks like a sql injection problem. The content of your variable $address seems to be interfering with the interpretation of the consultation. From a certain point of view, I’m glad it didn’t work: you don’t want this vulnerability on your system. It sucks.

  • 1

    @Diegorafaelsouza Syntax problem does not refer to variable $address. You may even have this problem too. But in this case, if you do more than one query with the function mysqli_query this problem will occur too! This question should not be closed.

  • 1

    @Andreicoelho I understood. I do not know of php, I will portray the signage. Thank you. I thought it could be the question of injection because in the message the instruction appears truncated.

  • Thank you, Diego. I didn’t notice that it was in Portuguese. I have seen that translated and sorry for it.

1 answer

0

Use the multi_query to do more than one query at the same time. So:

function newUser($connect, $name, $reg, $address, $phone, $email, $pass){
    $query = "insert into user(email, password) values('{$email}','{$pass}'); 
              insert into client(name, reg, address, phone) values('{$name}','{$reg}','{$address}','{$phone}')";

    return mysqli_multi_query($connect, $query); // aqui será realizado as duas querys
}

This syntax error occurs precisely because you are trying to perform more than one query using the function mysqli_query.

  • 1

    Thanks, Andrei! I didn’t know about this feature. Anyway, the beginning is just like that :)

  • @Rafaelfigueiredo tranquil! Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.