Is it possible to query two different tables from the same php file. It does work if I run one INSERT only but with two I cannot get it work. Why? Whats the way to use 2 INSERT statement in the same PHP page and add the information in two different tables?
<?php
include("../includes/connection.php");
// Escape user inputs for security
$name = mysqli_real_escape_string($link, $_POST['name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$number = mysqli_real_escape_string($link, $_POST['number']);
$device = mysqli_real_escape_string($link, $_POST['device']);
$price = mysqli_real_escape_string($link, $_POST['price']);
$payment = mysqli_real_escape_string($link, $_POST['payment']);
$status = mysqli_real_escape_string($link, $_POST['status']);
$model = mysqli_real_escape_string($link, $_POST['model']);
$problem = mysqli_real_escape_string($link, $_POST['problem']);
// attempt insert query execution
$sql = "INSERT INTO table1 (mail, number, device, price, paymenttype,status,date) VALUES ('$name', '$email', '$number', '$device', '$price', '$payment','$status',NOW())";
if(mysqli_query($link, $sql)){
// echo "Records added successfully.";
header("location:ciao.php?message=The customer has been added to the database");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
$sql = "INSERT INTO table2(model, problem, device, status) VALUES ('$model', '$problem', '$device', '$status')";
if(mysqli_query($link, $sql)){
// echo "Records added successfully.";
header("location:ciao.php?message=The customer has been added to the database");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
1) Check your first query syntax. It should be like:
$sql = "INSERT INTO table1(mail, number, device, price, paymenttype,status,date) VALUES ('$name', '$email', '$number', '$device', '$price', '$payment','$status',NOW())";
2) You have closed the connection before second insert query.