4

I have two form in my page. In first form user enters the details and submits it. Then the record will be saved in a database table. Then after from a button click I want to set a value from the data set which has been sent to the database to second form's hidden input. So for that when user submits the record from first form I am generating a unique id and sending it to the url. And also I am saving the unique id in my database table. Then after wrote the query to find the relevant data from relevant table using the unique id which is in url set the value to hidden input in my second form. But it's not working, I am providing my code here. Many thanks.

This is the action which will be performed from the first form's submission.

    <?php
require_once('dbh.inc_2.php');

if (isset($_POST['confirm'])) {

    $f_name=$_POST["first_name"];
    $l_name=$_POST["last_name"];
    $email=$_POST["email"];
    $nationality=$_POST["nationality"];
    $start_date=$_POST["date_start"];
    $type=$_POST["package"];
    $heads=$_POST["heads"];
    $tot_amount=$_POST["total"];
    $payment_amount=$_POST["advance"];
    $unique_id = uniqid();

    if (empty($f_name) || empty($l_name) || empty($email) || empty($nationality)|| empty($start_date) || empty($type) || empty($heads) || empty($tot_amount) || empty($payment_amount)) {
    header("Location: ../book_trip.php?error=emptyfields&please_fill_all_the_fields");
    exit();
    }
    else {
        $sql ="INSERT INTO bookings(f_name, l_name, email, nationality, s_date, type, heads, tot_amount, payment, ui) VALUES('$f_name','$l_name','$email', '$nationality', '$start_date', '$type', '$heads', '$tot_amount', '$payment_amount', '$unique_id')";



        if (mysqli_query($conn, $sql)) 
        {
            $m= "Booking placed";
        } 
        else
        {
            $m= "Error: " . $sql . "<br>" . mysqli_error($conn);
        }
        require_once('PHPMailer/PHPMailerAutoload.php');

            $mail = new PHPMailer();
            $mail -> isSMTP();
            $mail -> SMTPAuth = true;
            $mail -> SMTPSecure = 'ssl';
            $mail -> Host = 'smtp.gmail.com';
            $mail -> Port = '465';
            $mail -> isHTML();
            $mail -> Username = '[email protected]';
            $mail -> Password = 'wolfpack@123';
            $mail -> SetFrom('[email protected]');
            $mail -> Subject = 'Booking Confirmation - WolfPack';
            $mail -> Body = 'Thank you for choosing us as your travel partner! We hope to provide you an amazing travel adventure. You will recieve an email shortly with up and down locations and time. ' ;
            $mail -> AddAddress($email);
            $mail -> Send();
    }
}

header("location:../book_trip.php?msg=$m?ui=$unique_id"); 

This is to set the unique id to a variable which is on url and search for the data in database table using the unique id:

    <?php
require_once 'includes_2/dbh.inc_2.php';
$d_name = "";
if (isset($_GET['ui'])) {
$dbook_id = $_GET['ui'];
$sql = mysqli_query($conn, "SELECT f_name FROM bookings WHERE ui=$dbook_id") or die (mysqli_error($conn));
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
    $d_name = $row["f_name"];       
}   }?>
7
  • which bit is not working? Commented Apr 19, 2019 at 4:48
  • You are on wrong track. While inserting a data it will throw a id of inserted record by which you can easily find the newly inserted data easily. No need to pass unique id in URL. Commented Apr 19, 2019 at 4:49
  • @tim the code part which is on second form's hidden input fields's value Commented Apr 19, 2019 at 4:59
  • @MayankDudakiya record id is being auto incremented, so how can I take it without perming it in the code? And I want specific data to each user. Commented Apr 19, 2019 at 5:01
  • 1
    Do you want to get the latest inserted record into the database right? This will give you latest inserted id mysqli_insert_id($con) Commented Apr 19, 2019 at 5:03

2 Answers 2

1

In your url below you have forget you have use ? to separate value parameter instead of &

header("location:../book_trip.php?msg=$m?ui=$unique_id"); 

just change that to below :

header("location:../book_trip.php?msg=$m&ui=$unique_id"); 

Also, declare all your variable , above your if (isset($_POST['confirm'])) so that you can use it anywhere in your code.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried and it throws an error saying : Unknown column '5cb9635f775f9' in 'where clause'
@Avatar 5cb9635f775f9' is a string, so needs to be quoted in the query
@Avatar change it to WHERE ui='$dbook_id'
@Swati please take a look on this question too. stackoverflow.com/questions/55757772/…
1

If you're using an AUTO_INCREMENT field, there's no need to generate a separate unique ID unless that's for a different reason. Immediately after the success of the INSERT query, use the following command to obtain the new AUTO_INCREMENT value from the table.

Using procedural style:

$uid = mysqli_insert_id($conn);  

Or using object-oriented style:

$uid = $conn->insert_id;  

Also, instead of passing the $uid to the URL, I recommend using session instead - a little bit more secure.

$_SESSION['uid'] = mysqli_insert_id($conn);
//or
$_SESSION['uid'] = $conn->insert_id;

In your hidden field:

<input type="hidden" name="dest_name" id="dest_name" value="<?= $_SESSION['uid'] ?>">

Hope this is what you're looking for.

3 Comments

thank you very much for your valuable reply. Now it's working properly and I am able to set the value to hidden input field. But the data is not showing in the paypal shopping cart. I've provided the code in question. Please check it out. As I am new to php I am unable to figure it out. Thanks in advance.
I was under the understanding that you were having issues with passing and accessing a unique id from one form to another. Now, it's a different question. I haven't used Paypal for a while now, so I don't know what fields will/can be propagated to Paypal's payment interface. You might want to consult Paypal's manual to see what fields you can use.
Thank you @Christian Hur. I raised another question for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.