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.. Quote 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 />"; } Quote 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. Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/112430-help-please/#findComment-577352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.