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
https://forums.phpfreaks.com/topic/112430-help-please/
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
https://forums.phpfreaks.com/topic/112430-help-please/#findComment-577191
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.