-1

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>
DarthCoder
  • 37
  • 6
  • See https://www.php.net/manual/en/mysqli.insert-id.php for `how do I get the newly created userID`. If the question is more specific to DOM outputting please clarify. This code is open to SQL injections. Use parameterized queries. – user3783243 Mar 20 '23 at 00:06
  • `mysqli_error()` will not work as the `mysqli_*` version of this requires a DB connection. You also should not throw errors to the console. Log them and view your logs. – user3783243 Mar 20 '23 at 00:07
  • @Phil Thanks for answering. The header adjustments seems easy to understand. But before I insert this, I just want to understand correctly, would I retrieve the value as such; value="insert_id; ?>" ?? – DarthCoder Mar 20 '23 at 00:08
  • 1
    You would be better using SESSIONs. With this approach anyone can manipulate DOM and assume another's role. Also, how does a user reupload anything, or it is a 1 time process? – user3783243 Mar 20 '23 at 00:08
  • you need to comment on the answer for the answer to receive notices. – user3783243 Mar 20 '23 at 00:11
  • Related: [Send Ajax post request and retrieve the inserted id](https://stackoverflow.com/q/40798479/2943403) and [How to get last inserted id using AJAX?](https://stackoverflow.com/q/34041357/2943403) and [How to access last inserted userid in ajax](https://stackoverflow.com/q/43339039/2943403) and [How can I get " last inserted id to database " in Jquery Success Method?](https://stackoverflow.com/q/25988753/2943403) and [Ajax, retrieving ID of insert from within function](https://stackoverflow.com/q/51499363/2943403) – mickmackusa Mar 20 '23 at 03:28
  • [jQuery/AJAX retrieving ID of newly inserted record and using this in HTML before page refresh](https://stackoverflow.com/q/16167890/2943403) and [Using returned ajax ID in other calls to database](https://stackoverflow.com/q/51503146/2943403) and [Get Data in Ajax Response After Successful Insert Query in Codeigniter](https://stackoverflow.com/q/54234332/2943403) and [CodeIgniter, Ajax... How to use insert_id()](https://stackoverflow.com/q/51492828/2943403) and [Jquery $ajax POST data and get a response mysqli_insert_id](https://stackoverflow.com/q/29143517/2943403) – mickmackusa Mar 20 '23 at 03:32
  • Very closely related: [how to redirect to different page through ajax in php](https://stackoverflow.com/q/45233945/2943403) and [jQuery AJAX Onchange Redirect Issue](https://stackoverflow.com/q/62047891/2943403) and [Ajax Redirect on new page and after display success message](https://stackoverflow.com/q/38410655/2943403) – mickmackusa Mar 20 '23 at 03:36
  • And [How to redirect URL by id in PHP while inserting data into database](https://stackoverflow.com/q/59217221/2943403) – mickmackusa Mar 20 '23 at 03:46
  • 1
    All duplicates provided are much worse than answer provided below. – Your Common Sense Mar 20 '23 at 04:43

1 Answers1

4

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'];
Phil
  • 157,677
  • 23
  • 242
  • 245