Jump to content

help please


dotBz

Recommended Posts

hi, any ideas how i can do this?

  • insert data into table (table has an auto_increment id as 1st column)
  • view the data that was inserted in another page redirected using header()

So far, I have this:

<?php
$sql = sprintf("INSERT INTO table (data_1, data_2) VALUES ('%s', '%s');", 'data', 'data');
if (mysql_query($sql)) header("Location: another_page.php?id=" . mysql_result(mysql_query("SELECT * FROM table ORDER BY id DESC;"), 0));
?>

I'm sure there's a better way. Thanks in advance..  ;D

Link to comment
Share on other sites

I haven't tested this code but the general jist of it is right. The function mysql_insert_id() makes retrieving the ID of the last insert you performed extra simple.

 

$sql 	= "INSERT INTO table (data_1, data_2) VALUES ('$data1', '$data2')";
$rs	= mysql_query($sql);

if ($rs)
{
$id = number_format(mysql_insert_id($rs), 0, "", "");

header("Location: another_page.php?id=".$id);
exit;	
}
else
{
echo "Record could not be added.<br />";
}

 

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.