Jump to content

Retrieve ID of previous insert and forward to url


shawndibble

Recommended Posts

I have a page that inserts information to a database. But right now, it just refreshes the page when done. What I want to do is for the user to be directed to a specific page with the ID of the last insert in the url.

 

As you can see, I am using the following

 

To pull the id from the last insert

$sID = mysql_insert_id();

 

To take the user to the next page after inserting the data

header ("Location: http://www.newsalesparadigm.com/ex-sample/pretest.php?id=$sID");

 

<div class="content">
      <?php
	if(isset($_POST['add']))
	{

	include 'config.php';
	include 'opendb.php'; 

	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$business = $_POST['business'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zip = $_POST['zip'];
	$country = $_POST['country'];
	$step = $_POST['step'];
	$comments = $_POST['comments'];

	$query = "INSERT INTO clients (firstname, lastname, business, phone, email, address, city, state, zip, country, step, comments) VALUES ('$firstname', '$lastname', '$business', '$phone', '$email', '$address', '$city', '$state', '$zip', '$country', '0', '$comments')";
	mysql_query($query);
	$sID = mysql_insert_id();

	include 'closedb.php';
	}
	?>
      <form method="post">
        <table cellpadding="0" cellspacing="0">
        <tr>
        <td><input name="firstname" type="text" id="firstname" value="First Name" size="13" />
        <input name="lastname" type="text" id="lastname" value="Last Name" size="13" /></td>
        <td><a href="clientlist.php" style="padding-left:0px;">Return to client list</a></td>
        </tr>
        </table>
        <input name="business" type="text" id="business" value="Business Name" size="30" />
        <input name="phone" type="text" id="phone" value="Phone Number" size="30" />
        <input name="email" type="text" id="email" value="Email Address" size="30" />
        <input name="address" type="text" id="address" value="Address" size="30" />
        <input name="city" type="text" id="city" value="City" size="20" />
        <input name="state" type="text" id="state" value="State" size="3" />
        <input name="zip" type="text" id="zip" value="Zip" size="12" />
        <input name="country" type="text" id="country" value="Country" size="11" />
        <textarea name="comments" id="comments" size="30" rows="4" wrap="virtual">Comments</textarea>
        <br />
        <input name="add" id="add" type="submit" value="SAVE" />
        <?php
  if(isset($_POST['add']))
  {
  echo "New User Added"; 
  header ("Location: http://www.newsalesparadigm.com/ex-sample/pretest.php?id=$sID"); 
  }
  ?>

 

Link to comment
Share on other sites

Move the header call to the end of the if statement.

 

<div class="content">
<?php
if(isset($_POST['add']))
{

include 'config.php';
include 'opendb.php'; 

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$business = $_POST['business'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$step = $_POST['step'];
$comments = $_POST['comments'];

$query = "INSERT INTO clients (firstname, lastname, business, phone, email, address, city, state, zip, country, step, comments) VALUES ('$firstname', '$lastname', '$business', '$phone', '$email', '$address', '$city', '$state', '$zip', '$country', '0', '$comments')";
mysql_query($query);
$sID = mysql_insert_id();

include 'closedb.php';
header("Location: http://www.newsalesparadigm.com/ex-sample/pretest.php?id=$sID"); 
}
?>
      <form method="post">
        <table cellpadding="0" cellspacing="0">
        <tr>
        <td><input name="firstname" type="text" id="firstname" value="First Name" size="13" />
        <input name="lastname" type="text" id="lastname" value="Last Name" size="13" /></td>
        <td><a href="clientlist.php" style="padding-left:0px;">Return to client list</a></td>
        </tr>
        </table>
        <input name="business" type="text" id="business" value="Business Name" size="30" />
        <input name="phone" type="text" id="phone" value="Phone Number" size="30" />
        <input name="email" type="text" id="email" value="Email Address" size="30" />
        <input name="address" type="text" id="address" value="Address" size="30" />
        <input name="city" type="text" id="city" value="City" size="20" />
        <input name="state" type="text" id="state" value="State" size="3" />
        <input name="zip" type="text" id="zip" value="Zip" size="12" />
        <input name="country" type="text" id="country" value="Country" size="11" />
        <textarea name="comments" id="comments" size="30" rows="4" wrap="virtual">Comments</textarea>
        <br />
        <input name="add" id="add" type="submit" value="SAVE" />

Link to comment
Share on other sites

You need to turn on error_reporting() so you can see the errors you are getting.

 

The very first line of your code is

<div class="content">

, since that is sent to the browser, you can NOT send any headers after it.  The header() call is failing.

 

Move that <DIV> below the end of the IF block so it is only sent if you don't do the INSERT

 

Also, always, ALWAYS add an exit() after a header() call that redirects.  Otherwise, PHP continues running your script even though the browser goes off and asks for a new page.

 

Link to comment
Share on other sites

Sorry, i am fairly new to php. I will look up what you said. Here is the code for the full page.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EXpediter</title>
<link media="screen" href="style.css" rel="stylesheet" type="text/css" />
<link media="handheld, tty" rel="stylesheet" type="text/css" href="device.css" />
<style type="text/css">
<!--
.style3 {font-size: 12px}
-->
</style>
</head>

<body>
<table cellspacing="0" cellpadding="0">
  <tr>
    <td rowspan="3" height="500"><img src="images/image_01.jpg" width="35" height="500" /></td>
    <td valign="top" height="59" style="max-height:59px;height:59px;"><img src="images/image_02.jpg" width="238" height="59" /></td>
    <td rowspan="3" headers="500"><img src="images/image_03.jpg" width="35" height="500" /></td>
  </tr>
  <tr>
    <td valign="top" height="315">
    <?php include 'menu.php'; ?>
      <div class="content">
      <?php
	if(isset($_POST['add']))
	{

	include 'config.php';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);


	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$business = $_POST['business'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zip = $_POST['zip'];
	$country = $_POST['country'];
	$step = $_POST['step'];
	$comments = $_POST['comments'];

	$query = "INSERT INTO clients (firstname, lastname, business, phone, email, address, city, state, zip, country, step, comments) VALUES ('$firstname', '$lastname', '$business', '$phone', '$email', '$address', '$city', '$state', '$zip', '$country', '0', '$comments')";
	mysql_query($query);  

	mysql_close($conn);
	}
	?>
      <form method="post">
        <table cellpadding="0" cellspacing="0">
        <tr>
        <td><input name="firstname" type="text" id="firstname" value="First Name" size="13" />
        <input name="lastname" type="text" id="lastname" value="Last Name" size="13" /></td>
        <td><a href="clientlist.php" style="padding-left:0px;">Return to client list</a></td>
        </tr>
        </table>
        <input name="business" type="text" id="business" value="Business Name" size="30" />
        <input name="phone" type="text" id="phone" value="Phone Number" size="30" />
        <input name="email" type="text" id="email" value="Email Address" size="30" />
        <input name="address" type="text" id="address" value="Address" size="30" />
        <input name="city" type="text" id="city" value="City" size="20" />
        <input name="state" type="text" id="state" value="State" size="3" />
        <input name="zip" type="text" id="zip" value="Zip" size="12" />
        <input name="country" type="text" id="country" value="Country" size="11" />
        <textarea name="comments" id="comments" size="30" rows="4" wrap="virtual">Comments</textarea>
        <br />
        <input name="add" id="add" type="submit" value="SAVE" />
        <?php
  if(isset($_POST['add']))
  {
  echo "New User Added"; 
  header ("Location: http://www.newsalesparadigm.com/ex-sample/pretest.php"); 
  }
  ?>
            </form></div>
<div class="footer">©<?php echo date(Y); ?> <em>The EXpediter</em> Morgen Facilitations,Inc.</div>   </td>
  </tr>
  <tr>
    <td valign="bottom" height="126"><img src="images/image_05.jpg" width="238" height="126" /></td>
  </tr>
</table>
</body>
</html>

 

Link to comment
Share on other sites

Its OK.  We were all new to PHP at some time.

 

First, since you are new, put these two lines at the top of any page that has php:

error_reporting(E_ALL);
ini_set('display_errors', 1);

that will tell PHP to show you any errors or warnings that result from your code.  This will help you find and fix bugs.

 

Second, when writing a page that posts to itself (as yours is doing), I highly recommend processing the post at the beginning of the script, then output the HTML at the end.  So it would look something like this:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_POST['MySubmitButtonName'])) {
  // process the submitted form here
}
// Now build the form 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- and so forth -->

 

This keeps your code logically grouped together so it is easier to develop and maintain.  Also, it will prevent the problem you are having.  The problem is that the header("Location: ...") call is telling PHP to send a header.  However, you have other output above that.  Once you send text (whether it is HTML or just output from an echo statement), PHP sends all of the headers before it.  After that you cannot send any more headers, so your attempt to redirect after the POST processing is failing; but you don't see the error message because you don't have them turned on.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.