简体   繁体   中英

Inserting the same data into two tables using prepared statement

There are similar questions on this site but non that deals with prepared statements.This is my attempt to replicate those solutions with prepared statements and it does not work. I'm trying to insert $username_ into the username column of two tables at the same time. The data is inserted into the first table successfully but not the second.

here is my code:

include_once 'dbh.inc.php';
/* insert data into "candidates" table*/
$stmt_values = $conn->prepare("INSERT INTO candidates (username, firstname, lastname,
password, coalition,program, starting_year, slogan, email) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt_values->bind_param("sssssssss", $username_, $fname_, $lname_,
$hashedPwd, $coalition_, $program_, $starting_year_, $slogan_, $email_);
/* hash the password */
$hashedPwd = password_hash($pw, PASSWORD_DEFAULT);
$fname_ = $_POST['fname'];
$lname_ = $_POST['lname'];
$username_ = $_POST['username'];
$email_ = $_POST['email'];
$coalition_ = $_POST['coalition'];
$program_ = $_POST['program'];
$starting_year_ = $_POST['starting_year'];
$slogan_ = $_POST['slogan'];
$stmt_values->execute();
/* insert the username into the candidates_answer table also. this table contains the
candidates answer to the questions for the voting machine */
$stmt_values_2 = $conn->prepare("INSERT INTO candidate_answers (username, q1, q2, q3, q4, q5, q6) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt_values_2->bind_param("sssssss",$username1_, $q1, $q2, $q3, $q4, $q5, $q6);
$username1_ = $_POST['username'];$q1='';$q2='';$q3='';$q4='';$q5='';$q6='';
$stmt_values_2->execute();
header("location: ../register.php?register=success!");
exit();

schema of second table: 模式 what is the reason that the data is not being inserted into the second table? thanks

Declare variables before using them, not after:

include_once 'dbh.inc.php';

/* hash the password and DECLARE VARIABLES*/
$hashedPwd = password_hash($pw, PASSWORD_DEFAULT);
$fname_ = $_POST['fname'];
$lname_ = $_POST['lname'];
$username_ = $_POST['username'];
$email_ = $_POST['email'];
$coalition_ = $_POST['coalition'];
$program_ = $_POST['program'];
$starting_year_ = $_POST['starting_year'];
$slogan_ = $_POST['slogan'];

/* insert data into "candidates" table*/
$stmt_values = $conn->prepare("INSERT INTO candidates (username, firstname, lastname,
password, coalition,program, starting_year, slogan, email) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt_values->bind_param("sssssssss", $username_, $fname_, $lname_,
$hashedPwd, $coalition_, $program_, $starting_year_, $slogan_, $email_);
$stmt_values->execute();
/* insert the username into the candidates_answer table also. this table contains the
candidates answer to the questions for the voting machine */
/* Declare variables */
$username1_ = $_POST['username'];
// Still hard to read and bad practice:
$q1 = $q2 = $q3 = $q4 = $q5 = $q6 = '';

$stmt_values_2 = $conn->prepare("INSERT INTO candidate_answers (username, q1, q2, q3, q4, q5, q6) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt_values_2->bind_param("sssssss",$username1_, $q1, $q2, $q3, $q4, $q5, $q6);
$stmt_values_2->execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM