Printing some results on screen when submit is selected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You totally do not have to use a header redirect for this. All you need to do is retrieve the last insert ID from the insert using mysql_insert_id() and if that is set to something, tell the code to show the next form and the value returned from mysql_insert_id(). Adding header redirects is pointless if you are staying on your own server.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Its Working Perfectly If One Pass the GET Variables Through URL
Then Whats The Problem You Think ??
Is It for Header ??
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Everah wrote:You totally do not have to use a header redirect for this. All you need to do is retrieve the last insert ID from the insert using mysql_insert_id() and if that is set to something, tell the code to show the next form and the value returned from mysql_insert_id(). Adding header redirects is pointless if you are staying on your own server.

Ok Thanks,

But I currently do use a header redirect, all my insert pages redirect the user to a blank form. So how would I work round this using my method?

Can you see anything wrong with the current code?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you are currently using the header redirect, then continue to use it. Just pass the last insert id to the next page in the query string (as has been suggested already). Something like:

Code: Select all

<?php
// Default id value
$last_id = 0;

// Insert data
$sql = "INSERT INTO `myTable` (`id`, `lastName`, `firstName`) VALUES ('', 'Jones', 'Billy Bob')";
$result = mysql_query($sql) or die(mysql_error());

// If the query actually inserted something...
if (mysql_affected_rows())
{
    // get the last insert id
    $last_id = mysql_insert_id();
}

if ($last_id != 0)
{
    header('Location: http://www.yourdomain.com/yourprocessin ... ?fetchid=' . $last_id);
}
else
{
    echo 'The last id was not updated!';
}
?>
Keep in mind that there are some things to watch for when using mysql_insert_id(). I would strongly suggest you read the PHP manual on it for more information.

On your resulting page that you are forwarded to, you grab the last inserted id by the $_GET array value:

Code: Select all

<?php
if (isset($_GET['fetchid']))
{
    // The querystring var was set, validate it then use it
    // I WILL LEAVE VALIDATION UP TO YOU
    $last_id = $_GET['fetchid'];

    // Carry on ...
}
?>
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Thanks for your reply.

Neel_Basu suggestion seems to work on his side.

So what is wrong with this.

Code: Select all

header ("location:http://www.soi.city.ac.uk/organisation/pl/CMS/Uorgs.html?Sent=true&id=$org_id");
} else {
    die ( mysql_error($link));
}
And

Code: Select all

if ((isset($_GET['Sent'])) and ($_GET['Sent'] == 'true')) { echo '<b><font color="#FF0000">New Organisation Added, Add Another? Remember to Search for the Organisation and note the OID before Adding the Person Linked to the created Organisation</font></b>Your org id is'.$_GET[id]; }
Why wont it print out the Org_id All I get is the entire message and "Your org id is"


I get where your coming from with your suggestion, but looking at my current code It seems I am only a little way off solving this but cant discover the error in these two bits code
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

neel_basu wrote:
neel_basu wrote:Save The Page Where The
<?php
if ((isset($_GET['Sent'])) and ($_GET['Sent'] == 'true')) { echo '<b><font color="#FF0000">New Organisation Added, Add Another? Remember to Search for the Organisation and note the OID before Adding the Person Linked to the created Organisation</font></b>Your org id is'.$_GET[id]; }
?>
Is As some_file_name.php

And Use
header ("location:http://www.soi.city.ac.uk/organisation/ ... id=$org_id")
I Am Telling You Again Just Do This Once What I Have Told You Before. Try This Ones
And I Think I Have Told You Before That I Was Using php extension And It was Working.Whats Your Problem If You Use php Extension
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

OK.

I have done this.

I created a new file called Uorgs.php And Copied all the code to it.

I changed the header redirect to this new file.

AND still the same result

Code: Select all

mysql_select_db("contact_management_system",$link) or die("Could not select database");
 $sql = "INSERT INTO organisations (org_id,person_id, orgname, web_url, notes ) VALUES ('$org_id','$person_id','$orgname','$web_url', '$notes')";
 if ($result = mysql_query($sql,$link)) {
    header ("location:http://www.soi.city.ac.uk/organisation/pl/CMS/Uorgs.php?Sent=true&id=$org_id");
} else {
    die ( mysql_error($link));
} 

?>

Code: Select all

<?php 
if ((isset($_GET['Sent'])) and ($_GET['Sent'] == 'true')) { echo '<b><font color="#FF0000">New Organisation Added, Add Another? Remember to Search for the Organisation and note the OID before Adding the Person Linked to the created Organisation</font></b>Your org id is'.$_GET[id]; } 
?>
It simply prints out the message but doesnt print the org_id
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Replace

Code: Select all

header ("location:http://www.soi.city.ac.uk/organisation/pl/CMS/Uorgs.php?Sent=true&id=$org_id");
With

Code: Select all

if($org_id || !empty($org_id) || isset($org_id))
  {
    header ("location:http://localhost/tmp_rapid_php/tmp_rcv.php?Sent=true&id=$org_id");
  }
else
  {
    echo "\$org_id&nbsp;Has No Value Assined";
  }
And Tell What Is Being Printed

//**************************//
//**This Is For Testing Purpouse**//
//*************************//
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Code: Select all

mysql_select_db("contact_management_system",$link) or die("Could not select database");
 $sql = "INSERT INTO organisations (org_id,person_id, orgname, web_url, notes ) VALUES ('$org_id','$person_id','$orgname','$web_url', '$notes')";
 if ($result = mysql_query($sql,$link)) {
    header ("location:http://www.soi.city.ac.uk/organisation/pl/CMS/Uorgs.php?Sent=true&id=$org_id");
} else {
    die ( mysql_error($link));
} 

if($org_id || !empty($org_id) || isset($org_id))
  {
    header ("location:http://localhost/tmp_rapid_php/tmp_rcv.php?Sent=true&id=$org_id");
  }
else
  {
    echo "\$org_id&nbsp;Has No Value Assined";
  }
?>
I used that and this is what printed
New Organisation Added, Add Another? Remember to Search for the Organisation and note the OID before Adding the Person Linked to the created OrganisationYour org id is
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

mohson wrote:

Code: Select all

mysql_select_db("contact_management_system",$link) or die("Could not select database");
 $sql = "INSERT INTO organisations (org_id,person_id, orgname, web_url, notes ) VALUES ('$org_id','$person_id','$orgname','$web_url', '$notes')";
 if ($result = mysql_query($sql,$link)) {
    header ("location:http://www.soi.city.ac.uk/organisation/pl/CMS/Uorgs.php?Sent=true&id=$org_id");
} else {
    die ( mysql_error($link));
} 

if($org_id || !empty($org_id) || isset($org_id))
  {
    header ("location:http://localhost/tmp_rapid_php/tmp_rcv.php?Sent=true&id=$org_id");
  }
else
  {
    echo "\$org_id&nbsp;Has No Value Assined";
  }
?>
I used that and this is what printed
New Organisation Added, Add Another? Remember to Search for the Organisation and note the OID before Adding the Person Linked to the created OrganisationYour org id is
No You Didn't Did That I Told You To Replace It Not To Add It
Replace It
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

After Replacing It Would Look Like Like This
==============================

Code: Select all

mysql_select_db("contact_management_system",$link) or die("Could not select database");
 $sql = "INSERT INTO organisations (org_id,person_id, orgname, web_url, notes ) VALUES ('$org_id','$person_id','$orgname','$web_url', '$notes')";
if ($result = mysql_query($sql,$link))
  {
    if($org_id || !empty($org_id) || isset($org_id))
      {
    header ("location:http://localhost/tmp_rapid_php/tmp_rcv.php?Sent=true&id=$org_id");
      }
    else
      {
    echo "\$org_id&nbsp;Has No Value Assined";
      }
  }
else
  {
    die (mysql_error($link));
  }
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

ooops sorry neel.

I did this

Code: Select all

mysql_select_db("contact_management_system",$link) or die("Could not select database");
 $sql = "INSERT INTO organisations (org_id,person_id, orgname, web_url, notes ) VALUES ('$org_id','$person_id','$orgname','$web_url', '$notes')";
 if ($result = mysql_query($sql,$link)) {
   if($org_id || !empty($org_id) || isset($org_id))
  {
    header ("location:http://localhost/tmp_rapid_php/tmp_rcv.php?Sent=true&id=$org_id");
  }
else
  {
    echo "\$org_id&nbsp;Has No Value Assined";
  }
} else {
    die ( mysql_error($link));
}
And got this:

Code: Select all

$org_id Has No Value Assined
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Yes!
Thats The Actual Thruth And That I suspected
$org_id Is Not Getting Any value Assigned On It
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

So I Think You have Understood Whats The Actuall Error
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

But it is the last 8 records have all got an org_id assigned to it the process code works fine and auto increments an org id, when I search for the record it clearly shows the assigned org_id from 761 to 769
Post Reply