I have variable from an HTML form that are currently being posted to one table in my database table.
I would like to post those same variables to other tables at the same time within the same function. Is this possible? Here is my current PHP function that is posting successfully to one table
<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
// Database connection
$conn = new mysqli('localhost','user','password','database');
if($conn->connect_error){
echo "$conn->connect_error";
die("Connection Failed : ". $conn->connect_error);
} else {
$stmt = $conn->prepare("insert into table1(var1, var2, var3) values(?, ?, ?)");
$stmt->bind_param("sss", $var1, $var2, $var3);
$execval = $stmt->execute();
echo $execval;
$stmt->close();
$conn->close();
}
?>
And I would like the following the variables to post to more than one table in the same database, so was thinking the following but it does not work -
<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
// Database connection
$conn = new mysqli('localhost','user','password','database');
if($conn->connect_error){
echo "$conn->connect_error";
die("Connection Failed : ". $conn->connect_error);
} else {
$stmt = $conn->prepare("insert into table1(var1, var2, var3) values(?, ?, ?)");
$stmt->bind_param("sss", $var1, $var2, $var3);
$stmt = $conn->prepare("insert into table2(var1) values(?)");
$stmt->bind_param("s", $var1);
$stmt = $conn->prepare("insert into table3(var2, var3) values(?, ?)");
$stmt->bind_param("ss", $var2, $var3);
$execval = $stmt->execute();
echo $execval;
$stmt->close();
$conn->close();
}
?>