cavey5 Posted April 23, 2007 Share Posted April 23, 2007 I have an INSERT that puts values into mt database, and a variable, a userid, is created by auto increment. Is it possible to do the insert, and then immediatly do a SELECT on the same page, to pull that userid out? What I have is echoing an empty variable, any advise? Why does this echo to getsubscriber_script.php?cm_id= with no value? [pre] // Aquires data source from form $cm_company = mysql_real_escape_string($_POST['cm_company']); $cm_email = mysql_real_escape_string($_POST['cm_email']); // Inserts data into database $addSubscriber = "INSERT INTO subscriber_data (company, email) VALUES ('$company', '$email')"; $addSubscriber_result= mysql_query($addSubscriber) OR die('QUERY ERROR:<br />' .$addSubscriber. '<br />' .mysql_error()); // Retreives data from database $getDetails = "(SELECT userid FROM subscriber_data WHERE email = '$cm_email')"; $getDetails_result = mysql_query($getDetails) OR die(mysql_error()); while ($row = mysql_fetch_array($getDetails)) { $userid = $row["userid"]; } // Routes user to details header("Location: getsubscriber_script.php?cm_id=$cm_id"); exit; ?> [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/48370-two-queries-on-a-page/ Share on other sites More sharing options...
Wildbug Posted April 24, 2007 Share Posted April 24, 2007 You can use either mysql_insert_id() (in PHP) or LAST_INSERT_ID() (in MySQL). Quote Link to comment https://forums.phpfreaks.com/topic/48370-two-queries-on-a-page/#findComment-236545 Share on other sites More sharing options...
cavey5 Posted April 24, 2007 Author Share Posted April 24, 2007 What is the syntax on that, i.e. where does it go. I cannot find any info on this. Quote Link to comment https://forums.phpfreaks.com/topic/48370-two-queries-on-a-page/#findComment-236599 Share on other sites More sharing options...
cavey5 Posted April 24, 2007 Author Share Posted April 24, 2007 There's no edit post function on these forums? Got it: $cm_id = mysql_insert_id(); header("Location: cm_subscriber_detail.php?cm_id=$cm_id"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/48370-two-queries-on-a-page/#findComment-236621 Share on other sites More sharing options...
cavey5 Posted April 24, 2007 Author Share Posted April 24, 2007 Now, how would you pass the value to the header redirect after an UPDATE, because that does not generate an auto-increment value, thus results in a value of 0. I need to redirect the user back to the Customer Details page using $_GET and the userid in the URL... but once the UPDATE has been ran, the $_GET value of $_GET['$cm_id']; seems to go null. See code and note in red. [pre] <?php // Makes initial connection to database define ('DB_USER', '***********'); define ('DB_PASSWORD', '******'); define ('DB_HOST', 'localhost'); define ('DB_NAME', '***********); $connect = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('215 Our database is currently down for updates, please check back later.'); // or die(mysql_error()); $db = @mysql_select_db(DB_NAME, $connect) or die('216 Our database is currently down for updates, please check back later.'); // Aquires data source from form $cm_id = mysql_real_escape_string($_POST['cm_id']); $cm_company = mysql_real_escape_string($_POST['cm_company']); $cm_phone = mysql_real_escape_string($_POST['cm_phone']); $cm_email = mysql_real_escape_string($_POST['cm_email']); $cm_firstname = mysql_real_escape_string($_POST['cm_firstname']); $cm_lastname = mysql_real_escape_string($_POST['cm_lastname']); $cm_address1 = mysql_real_escape_string($_POST['cm_address1']); $cm_address2 = mysql_real_escape_string($_POST['cm_address2']); $cm_city = mysql_real_escape_string($_POST['cm_city']); $cm_state = mysql_real_escape_string($_POST['cm_state']); $cm_zipcode = mysql_real_escape_string($_POST['cm_zipcode']); $cm_country = mysql_real_escape_string($_POST['cm_country']); $cm_subtype = mysql_real_escape_string($_POST['cm_subtype']); $cm_source = mysql_real_escape_string($_POST['cm_source']); $cm_payment = mysql_real_escape_string($_POST['cm_payment']); $cm_code = mysql_real_escape_string($_POST['cm_code']); $cm_startissue = mysql_real_escape_string($_POST['cm_startissue']); $cm_endissue = mysql_real_escape_string($_POST['cm_endissue']); $cm_status = mysql_real_escape_string($_POST['cm_status']); $cm_notes = mysql_real_escape_string($_POST['cm_notes']); // Updates database $updateSubscriber = "UPDATE subscriber_data SET cm_company = '$cm_company', cm_phone = '$cm_phone', cm_email = '$cm_email', cm_firstname = '$cm_firstname', cm_lastname = '$cm_lastname', cm_address1 = '$cm_address1', cm_address2 = '$cm_address2', cm_city = '$cm_city', cm_state = '$cm_state', cm_zipcode = '$cm_zipcode', cm_country = '$cm_country', cm_subtype = '$cm_subtype', cm_source = '$cm_source', cm_payment = '$cm_payment', cm_code = '$cm_code', cm_startissue = '$cm_startissue', cm_endissue = '$cm_endissue', cm_status = '$cm_status' WHERE cm_id = '$cm_id'"; $updateSubscriber_result= mysql_query($updateSubscriber) OR die('QUERY ERROR:<br />' .$updateSubscriber. '<br />' .mysql_error()); header("Location: cm_subscriber_detail.php?cm_id=$cm_id"); <-- How do I get the value of $cm_id into this string? exit; ?> [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/48370-two-queries-on-a-page/#findComment-236651 Share on other sites More sharing options...
cavey5 Posted April 24, 2007 Author Share Posted April 24, 2007 Don't be shy! I need help here! Quote Link to comment https://forums.phpfreaks.com/topic/48370-two-queries-on-a-page/#findComment-237142 Share on other sites More sharing options...
Wildbug Posted April 24, 2007 Share Posted April 24, 2007 Are you using sessions? If so, save it in a session variable. Quote Link to comment https://forums.phpfreaks.com/topic/48370-two-queries-on-a-page/#findComment-237166 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.