I added this one statement that messed me up so bad. All I was trying to do was keep the mysql user table id number (auto inc) to use later on another page. The redirect stopped working and “undefined” started showing up instead. I also noticed my auto incr counter started to increase by 2 instead of one.
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "<script>document.write(localStorage.setItem('UserID', '.$last_id.'))</script>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
This is the php file i am using.
<?php
$servername = "test.ipagemysql.com";
$username = "user";
$password = "password";
$dbname = "churchpal";
$F_Name= $_POST['first_name'];
$L_Name= $_POST['last_name'];
$Pass_Hash= $_POST['user_pass'];
$Phone= $_POST['phone'];
$Email= $_POST['email'];
error_reporting (E_ALL ^ E_NOTICE);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die();
}
// echo "Connected successfully...";
// Stops the entering of a blank records into user table.
if(empty($_POST['first_name'])){
die();
}
$sql = "INSERT INTO $user_table (first_name, last_name, PasswordHash, phone, email)
VALUES ('$F_Name', '$L_Name', '$Pass_Hash', '$Phone', '$Email')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "<script>document.write(localStorage.setItem('UserID', '".$last_id."'))</script>";
// echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
// echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
header("Location: http://praypal.club/userthankyou.html"); exit;
$conn->closed();
?>