I have a register page that I initially had one query going to a db table called 'users'. In the 'users' table I have a column called 'id' that is a primary auto increment field.
I then created a new query under my original that will send to a db table called 'payment status'. Everything in this query is working other than my attempt to use the 'id' created by the 'users' table and to put that into my 'payment_status' db table for the column 'user_id'. It always enters a 0 value in it.
if($validation->passed()) {
$user = new User();
$salt = Hash::salt(32);
try {
$user->create(array(
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'email' => Input::get('email'),
'phone_number' => Input::get('phone_number'),
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'), $salt),
'salt' => $salt,
'joined' => date('Y-m-d H:i:s'),
'group' => 1
));
$success = "You have successfully created an account. We will notify you once the account has been approved. Then you will be able to login.";
echo $success;
} catch(Exception $e) {
die($e->getMessage());
}
} else {
foreach($validation->errors() as $error) {
$error;
}
}
The second query...
if(isset($_POST['submit'])){
$id = ( isset( $_SESSION['id'] ) ? $_SESSION['id'] : "" );
$user_id = ( isset( $_SESSION['id'] ) ? $_SESSION['id'] : "" );
$firstname = Input::get('firstname');
$payment_name = "Owes";
$payment_id = 1;
$payment_amount = 0;
//Query to add user's name to owes db table
$con = mysqli_connect("localhost","root","","db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$stmt = $con->prepare("INSERT INTO payment_status (id, user_id, firstname, payment_name, payment_id, payment_amount) VALUES (?, ?, ?, ?, ?, ?)");
if ( false===$stmt ) {
// Check Errors for prepare
die(' Owes DB prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt->bind_param('iissii', $id, $user_id, $firstname, $payment_name, $payment_id, $payment_amount);
if ( false===$stmt ) {
// Check errors for binding parameters
die('Owes DB bind_param() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->execute();
if ( false===$stmt ) {
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
}
I'm attempting to get the 'id' from the 'users' table like this...
$user_id = ( isset( $_SESSION['id'] ) ? $_SESSION['id'] : "" );
So I am trying to get the 'id' from the 'users' table and use that for the 'user_id' in the 'payment_status' table.
Does anyone see what I am doing wrong?
First you're not initializing (in the code you show) the SESSION_ID
of the new user. You inserted successfully the user into the table users
but you didn't start a session for him.
Secondly, if you want to get the id of the last user you might want to use mysqli_insert_id.