dotBz Posted June 29, 2008 Share Posted June 29, 2008 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.. Link to comment https://forums.phpfreaks.com/topic/112430-help-please/ Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 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 More sharing options...
wildteen88 Posted June 29, 2008 Share Posted June 29, 2008 Why use number_format? No need to use it as mysql_insert_id returns an integer, not a float. Link to comment https://forums.phpfreaks.com/topic/112430-help-please/#findComment-577350 Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Why use number_format? No need to use it as mysql_insert_id returns an integer, not a float. You're right, that is superflous in this situation. Link to comment https://forums.phpfreaks.com/topic/112430-help-please/#findComment-577352 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.