After my users submit some basic info in a form, how do I get the newly created userID to be echoed back to a hidden field on the header (redirect) page?
The first form contains basis Name, Phone, Email inputs. When user submits form (to database), the AI 'userID' column generates their unique 'userID'.
I need that unique ID to echo back to a hidden field on the header (redirect) page where the user will then upload their profile photo.
New User Form:
<h1>New User Form</h1>
<form action="newUser_formHandler.php" method="POST" >
<input name="userName" placeholder="Enter Name"><br><br>
<input name="userPhone" placeholder="Enter Phone"><br><br>
<input name="userEmail" placeholder="Enter Email"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
New User Form Handler:
<?php include './secure/dbKey.php';?>
<?php
if(isset($_POST['submit'])){
$userName = $_POST['userName'] ;
$userPhone = $_POST['userPhone'] ;
$userEmail = $_POST['userEmail'] ;
$newUser = "insert into Users (userName, userPhone, userEmail)
values('$userName', '$userPhone', '$userEmail')";
$run = mysqli_query($conn,$newUser) or die(mysqli_error()) ;
//Page Re-direct after form submission
header('location: /profilePhoto_UploadPage.php');
}
?>
Profile Photo Upload Page:
<?php include './secure/dbKey.php';?>
<html>
<head>
<title>Profile Photo Upload Page</title>
</head>
<body>
<h1>Congratulations!</h1>
<h2>Your profile was succesfully created!</h2>
<a>Please upload a photo to complete your profile:</a><br><br>
<form action="photoUpload_Handler.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="userID" id="userID" value="<?php echo $userID; ?>">
<input type="file" name="fileToUpload" id="fileToUpload"><br><br>
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Retrieve the value using $conn->insert_id
and either pass it in the query string or set it into the session.
Also, whatever tutorial or guide you're following is seriously out-of-date or just plain bad. Use prepared statements and follow a good tutorial
$stmt = $conn->prepare(
"INSERT INTO `Users` (`userName`, `userPhone`, `userEmail`) VALUES (?, ?, ?)"
);
$stmt->bind_param(
"sss",
$_POST["userName"],
$_POST["userPhone"],
$_POST["userEmail"]
);
$stmt->execute();
header(
"Location: /profilePhoto_UploadPage.php?" .
http_build_query(["userID" => $conn->insert_id])
);
exit();
On the secondary page you'd use the query parameter
<input
type="hidden"
name="userID"
id="userID"
value="<?= htmlspecialchars($_GET['userID']) ?>"
/>
Using a session is just as easy
session_start();
// same mysqli code as above
$_SESSION['userID'] = $conn->insert_id;
header('Location: /profilePhoto_UploadPage.php');
exit;
On the secondary page you wouldn't really need it as whatever page you're posting to would also be able to read the session
// photoUpload_Handler.php
session_start();
$userID = $_SESSION['userID'];