-1

For page 2 of a multipage form, I'm trying to figure out how to get an ID from my db and appending this ID to the URL parameter. In my db, the ID field is set as an auto increment.

So on page 1, I've got the form set as

<form class="form-horizontal" role="form" name="assessment" action="assessment-page2.php" method="POST">

<div class="form-group" style="margin-bottom:10px;">
<label for="company_name" class="col-lg-1 col-md-2 col-sm-4 control-label"><strong>Company name:</strong></label>
<div class="col-md-2"> <input type="text" id="company_name" name="company_name"  class="form-control" required> </div>
  <div class="clearfix"><br /></div>
<label for="date" class="col-lg-1 col-md-2 col-sm-4 control-label"><strong>Date:</strong></label>
 <div class="col-md-2"><input type="date" name="date" id="date" class="form-control" required> </div>
 <div class="clearfix"><br /></div>
 <label for="rsm" class="col-lg-1 col-md-2 col-sm-4 control-label"><strong>Regional Sales Manager:</strong></label>
<div class="col-md-2"> <input type="text" name="rsm" id="rsm" class="form-control" required></div>
<div class="clearfix"><br /></div>
<label for="agents" class="col-lg-1 col-md-2 col-sm-4 control-label"><strong>Agents:</strong> </label>
<div class="col-md-2"> <input type="text" name="agents" id="agents" class="form-control" required></div>
<div class="clearfix"><br /></div>
<label for="distributor" class="col-lg-1 col-md-2 col-sm-4 control-label"><strong>Distributor:</strong> </label>
<div class="col-md-2"><input type="text" name="distributor" id="distributor" class="form-control" required></div>
</div>

<p style="margin-left:5px;"><input class="btn btn-danger" type="reset"> 
<input style="margin-left:10px;" class="btn btn-success" type="submit" value="Next" name="next" /></p>

Now on page 2, I'm inserting the page 1 information into its unique table in the db (and this is where the ID field is).

So after this is done, I can do a quick echo for this field in the db and I do get the ID number as expected. Now how do I pass this information to the header location so the url on page 2 becomes /assessment-page2.php?id=n

tim r
  • 89
  • 11
  • 1
    That really depends on how you are inserting the record. Are you using PDO or MySQLi? http://stackoverflow.com/questions/10680943/pdo-get-the-last-id-inserted or http://stackoverflow.com/questions/19738283/mysqli-last-insert-id take your pick – rjdown Oct 05 '16 at 21:03
  • Thanks @rjdown The answer to this was quickly solved using http://stackoverflow.com/questions/19738283/mysqli-last-insert-id – tim r Oct 05 '16 at 21:05

1 Answers1

0

After you execute the query to do the insert, grab the ID of the last insert like this. . .

$id = mysqli_insert_id($DBLINK);

You could pass the $id via the action tag

action="assessment-page2.php?id=<?=$id?>"

or, use it to populate a hidden variable in the form..

<input type="hidden" name="id" value="<?=$id?>">
Duane Lortie
  • 1,285
  • 1
  • 12
  • 16