The basic idea is to take contact info as well as vehicle info from a single html form, and using a single php file, insert the corresponding data into a 'member' table and 'vehicle' table respectively.
I've looked at a few somewhat similar examples and haven't got them to work for me. Is this possible to do? Or would I need to separate the requested info into 2 different forms and handle via 2 php files?
I'm using phpMyAdmin with php and PDO.
User entered info stored as an array and binded via a for loop.
// DB Interaction
try{
// Connection to DB
require "includes/db-info.php";
$dbh = new PDO("mysql:host=$serverName; dbname=$dbName", $userName, $password);
// Inserts into member and vehicle table
$stmt = $dbh->prepare("INSERT INTO member (first_name, last_name, address, state, zip_code, email, password, phone_number, member_status,
member_type, insurance_provider, policy_number, number_of_claims, business_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
INSERT INTO vehicle (member_id, make, model, year, color, vin)
VALUES (?, ?, ?, ?, ?, ?)");
// Inserts array elements into the placeholders
for ($i = 1; $i < 22; $i++){
$stmt->bindParam($i, $memberFormElements[$i]);
}
$stmt->execute();
$stmt = null;
$dbh = null;
} catch(PDOException $e){
echo $stmt . "<br/>" . $e->getMessage();
die();
}
I've also tried separating the two queries and assigning different variable names before proceeding to bind, but it ends up only entering data into the second 'vehicle' table.
// DB Interaction
try{
// Connection to DB
require "includes/db-info.php";
$dbh = new PDO("mysql:host=$serverName; dbname=$dbName", $userName, $password);
// Inserts into member table
$stmt = $dbh->prepare("INSERT INTO member (first_name, last_name, address, state, zip_code, email, password, phone_number, member_status,
member_type, insurance_provider, policy_number, number_of_claims, business_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
// Inserts array elements into the placeholders
// Starts at 1 due to 'prepare' indexing. Array accounted for.
for ($i = 1; $i < 16; $i++){
$stmt->bindParam($i, $memberFormElements[$i]);
}
$stmt->execute();
$stmt2 = dbh->prepare("INSERT INTO vehicle (member_id, make, model, year, color, vin)
VALUES (?, ?, ?, ?, ?, ?)");
// Inserts array elements into the placeholders
// Starts at 1 due to 'prepare' indexing. Array accounted for.
for ($i = 1; $i < 7; $i++){
$stmt2->bindParam($i, $vehicleFormElements[$i]);
}
$stmt2->execute();
$stmt = null;
$dbh = null;
} catch(PDOException $e){
echo $stmt . "<br/>" . $e->getMessage();
die();
}