I'm working on a project and am having some troubles with PDO.
Here is my code:
try {
$dbc = new PDO("mysql:host=$host;dbname=$dbname",$username,$password);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Database is now connected. Let's prepare the statement next.
$stmt = $dbc->prepare("INSERT INTO users (username, password, email, gender) VALUES (?, ?, ?, ?)");
//Next, bind parameters.
$stmt->bindParam(1, $input_username);
$stmt->bindParam(2, $input_password);
$stmt->bindParam(3, $input_email);
$stmt->bindParam(4, $input_gender);
$stmt->execute();
//We just added information into the members table. Done!
//We need to get the new user's ID, and save it in a variable so we can JOIN the tables later.
$userID;
$stmt2 = $dbc->prepare("SELECT id FROM users WHERE username='$input_username'");
$stmt2->execute();
$stmt2->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt2->fetch()) {
$userID = $row['id'];
}
//We now how the ID of the user. Done!
//Now, let's insert information into the birthdates table.
$stmt3 = $dbc->prepare("INSERT INTO birthdates (id, month, day, year) VALUES (?, ?, ?, ?)");
$stmt3->bindParam(1, $userID);
$stmt3->bindParam(2, $input_dateofbirth_month);
$stmt3->bindParam(3, $input_dateofbirth_day);
$stmt3->bindParam(4, $input_dateofbirth_year);
$stmt3->execute();
//Done adding the date of birth to the birthdates table!
//Let's add the postal code and location to the location table.
$stmt4 = $dbc->prepare("INSERT INTO location (id, postalcode, location) VALUES (?, ?, ?)");
$stmt4->bindParam(1, $userID);
$stmt4->bindParam(2, $input_postalcode);
$stmt4->bindParam(3, $input_location);
$stmt4->execute();
//BOOM! All of the information is now done and in a database.
} catch (PDOException $e) {
$e->getMessage();
}My code doesn't update all of the tables everytime. Sometimes it doesn't update birthdates, and sometimes it doesn't update users, etc.
Is there something I'm missing? Surely it must be a setting or something.
Thanks!

New Topic/Question
Reply




MultiQuote






|