P
1. If the table is in the same databaseJust create the other query and run it, using the same connection$sql = "INSERT INTO cuestionario (r1, r2, r3, r4, r5, r6) VALUES ('$q1', '$q2', '$q3', '$q4', '$q5', '$q6')";
$rsCuestionario = $mysqli->query($sql);
/*
Esta sería la segunda consulta. Si las columnas se llaman
distinto de r1, r2 ... debes cambiar su nombre
*/
$sql = "INSERT INTO otraTabla (r1, r2, r3, r4, r5, r6) VALUES ('$q1', '$q2', '$q3', '$q4', '$q5', '$q6')";
$rsOtraTabla = $mysqli->query($sql);
There you will have two references to each execution you can use for whatever it takes, such as evaluating whether the row was actually inserted, etc.2. If the tables are in different databasesAll the same, only you will have to connect to the other database for the other insertion.require 'otraconexion.php';
#Esta es la segunda inserción, la primera queda tal cual
#Suponemos que en ese archivo la conexión se llama $mysqliOther
$sql = "INSERT INTO otraTabla (r1, r2, r3, r4, r5, r6) VALUES ('$q1', '$q2', '$q3', '$q4', '$q5', '$q6')";
$rsOtraTabla = $mysqliOther->query($sql);
3. A complete example by controlling eventual errorsHere we evaluate the status of both calls to querychecking if there's a mistake or not. It saves everything in a variable $arrData which contains two keys, error and msg which will be used at the end to show what has happened.#Ponemos claves en el array array para ir concatenando los eventuales mensajes y errores
$arrData['msg']="";
$arrData['error']="";
$sql = "INSERT INTO cuestionario (r1, r2, r3, r4, r5, r6) VALUES ('$q1', '$q2', '$q3', '$q4', '$q5', '$q6')";
$rsCuestionario = $mysqli->query($sql);
#Verificamos si hubo error en la consulta y almacenamos el error si lo hubo o la cantidad de filas insertadas
( $rsCuestionario ) ? $arrData['msg'].=sprintf("Se insertaron %d filas en cuestionario\n", $mysqli->affected_rows) : $arrData['error']=sprintf("Error en cuestionario: %s\n", $mysqli->error);
/*
Esta sería la segunda consulta. Si las columnas se llaman
distinto de r1, r2 ... debes cambiar su nombre
*/
$sql = "INSERT INTO otraTabla (r1, r2, r3, r4, r5, r6) VALUES ('$q1', '$q2', '$q3', '$q4', '$q5', '$q6')";
$rsOtraTabla = $mysqli->query($sql);
#Verificamos si hubo error en la consulta y almacenamos el error si lo hubo o la cantidad de filas insertadas
( $rsOtraTabla ) ? $arrData['msg'].=sprintf("Se insertaron %d filas en otherTable\n", $mysqli->affected_rows) : $arrData['error'].=sprintf("Error en otherTable: %s\n", $mysqli->error);
/*
MUY IMPORTANTE: Ahora hacemos una prueba sobre $arrData
que consistirá en verificar si existe una clave error en el array, imprimiendo dicho error
o los mensajes de éxito para cada consulta según el caso.
*/
$msg= (!empty($arrData['error'])) ? $arrData['error'] : $arrData['msg'];
echo $msg;
NotesIn mysqli There is also a multiquery option, but right now I don't remember if it works with different tables. I don't think it's worth making use of it either, because if I don't remember in some contexts it needs to be configured.As for security, I would like to point out that your consultation with the highly vulnerable to SQL Injection attacks. A malicious user can manipulate the variables and produce a harmful code for the system or for the database (see this question: https://es.stackoverflow.com/q/108520/29967 to understand the level of gravity that this might have, even if they are test boards). To neutralize this risk, use prepared consultations. I recommend you read on the subject whenever possible.