dflow Posted November 26, 2008 Share Posted November 26, 2008 i created an insert record form i choose parameters and want the page to go to the update of the last record inserted , according to the $ProductID variable i use: <?php // get the last auto incremented value $ProductID = mysql_insert_id(); ?> and this is the after insert goto link: $insertGoTo = "http://mysite.com/backoffice/Manager_control_panel/update_product_form.php?PID=" . $ProductID . ""; the link doesnt get the variable as a parameter here is the whole code: <?php require_once('../../Connections/international.php'); ?> <?php mysql_query("SET NAMES 'utf8'")?> <?php // get the last auto incremented value $ProductID = mysql_insert_id(); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO products (CategoryID, RegionID, CityID, CountryID) VALUES (%s, %s, %s, %s)", GetSQLValueString($_POST['CategoryID'], "int"), GetSQLValueString($_POST['RegionID'], "int"), GetSQLValueString($_POST['CityID'], "int"), GetSQLValueString($_POST['CountryID'], "int")); mysql_select_db($database_international, $international); $Result1 = mysql_query($insertSQL, $international) or die(mysql_error()); $insertGoTo = "http://mysite.com/backoffice/Manager_control_panel/update_product_form.php?PID=" . $ProductID . ""; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_international, $international); $query_RsProducts = "SELECT * FROM products"; $RsProducts = mysql_query($query_RsProducts, $international) or die(mysql_error()); $row_RsProducts = mysql_fetch_assoc($RsProducts); $totalRows_RsProducts = mysql_num_rows($RsProducts); mysql_select_db($database_international, $international); $query_RsCitylist = "SELECT * FROM city_list"; $RsCitylist = mysql_query($query_RsCitylist, $international) or die(mysql_error()); $row_RsCitylist = mysql_fetch_assoc($RsCitylist); $totalRows_RsCitylist = mysql_num_rows($RsCitylist); mysql_select_db($database_international, $international); $query_RsCountryList = "SELECT * FROM country_list"; $RsCountryList = mysql_query($query_RsCountryList, $international) or die(mysql_error()); $row_RsCountryList = mysql_fetch_assoc($RsCountryList); $totalRows_RsCountryList = mysql_num_rows($RsCountryList); mysql_select_db($database_international, $international); $query_RsRegionList = "SELECT * FROM region_list"; $RsRegionList = mysql_query($query_RsRegionList, $international) or die(mysql_error()); $row_RsRegionList = mysql_fetch_assoc($RsRegionList); $totalRows_RsRegionList = mysql_num_rows($RsRegionList); mysql_select_db($database_international, $international); $query_RsSupplierList = "SELECT * FROM suppliers"; $RsSupplierList = mysql_query($query_RsSupplierList, $international) or die(mysql_error()); $row_RsSupplierList = mysql_fetch_assoc($RsSupplierList); $totalRows_RsSupplierList = mysql_num_rows($RsSupplierList); mysql_select_db($database_international, $international); $query_RsCategorylist = "SELECT * FROM category_list"; $RsCategorylist = mysql_query($query_RsCategorylist, $international) or die(mysql_error()); $row_RsCategorylist = mysql_fetch_assoc($RsCategorylist); $totalRows_RsCategorylist = mysql_num_rows($RsCategorylist); mysql_select_db($database_international, $international); $query_RsNumberList = "SELECT * FROM nums"; $RsNumberList = mysql_query($query_RsNumberList, $international) or die(mysql_error()); $row_RsNumberList = mysql_fetch_assoc($RsNumberList); $totalRows_RsNumberList = mysql_num_rows($RsNumberList); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Add new product start page</title> </head> <body> <form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1"> <table align="center"> <tr valign="baseline"> <td colspan="2" align="right" nowrap="nowrap">ADD NEW PRODUCT</td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right">CategoryID:</td> <td><select name="CategoryID" id="CategoryID"> <option value="Choose Category">Choose Category</option> <?php do { ?><option value="<?php echo $row_RsCategorylist['CategoryID']?>"><?php echo $row_RsCategorylist['CategoryName_heb']?></option> <?php } while ($row_RsCategorylist = mysql_fetch_assoc($RsCategorylist)); $rows = mysql_num_rows($RsCategorylist); if($rows > 0) { mysql_data_seek($RsCategorylist, 0); $row_RsCategorylist = mysql_fetch_assoc($RsCategorylist); } ?> </select></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right">CountryID:</td> <td><select name="CountryID" id="CountryID"> <option value="Choose Country">Choose Country</option> <?php do { ?> <option value="<?php echo $row_RsCountryList['CountryID']?>"><?php echo $row_RsCountryList['CountryName_heb']?></option> <?php } while ($row_RsCountryList = mysql_fetch_assoc($RsCountryList)); $rows = mysql_num_rows($RsCountryList); if($rows > 0) { mysql_data_seek($RsCountryList, 0); $row_RsCountryList = mysql_fetch_assoc($RsCountryList); } ?> </select></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right">RegionID:</td> <td><select name="RegionID" id="RegionID"> <option value="Choose Region">Choose Region</option> <option value="0">None</option> <?php do { ?> <option value="<?php echo $row_RsRegionList['RegionID']?>"><?php echo $row_RsRegionList['RegionName']?></option> <?php } while ($row_RsRegionList = mysql_fetch_assoc($RsRegionList)); $rows = mysql_num_rows($RsRegionList); if($rows > 0) { mysql_data_seek($RsRegionList, 0); $row_RsRegionList = mysql_fetch_assoc($RsRegionList); } ?> </select></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right">CityID:</td> <td><select name="CityID" id="CityID"> <option value="Choose City">Choose City</option> <option value="0">None</option> <?php do { ?> <option value="<?php echo $row_RsCitylist['CityID']?>"><?php echo $row_RsCitylist['CityName_heb']?></option> <?php } while ($row_RsCitylist = mysql_fetch_assoc($RsCitylist)); $rows = mysql_num_rows($RsCitylist); if($rows > 0) { mysql_data_seek($RsCitylist, 0); $row_RsCitylist = mysql_fetch_assoc($RsCitylist); } ?> </select></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right"> </td> <td><input type="submit" value="Insert record" /></td> </tr> </table> <input type="hidden" name="MM_insert" value="form1" /> </form> <p> </p> </body> </html> <?php mysql_free_result($RsProducts); mysql_free_result($RsCitylist); mysql_free_result($RsCountryList); mysql_free_result($RsRegionList); mysql_free_result($RsSupplierList); mysql_free_result($RsCategorylist); mysql_free_result($RsNumberList); ?> Link to comment https://forums.phpfreaks.com/topic/134382-insertgoto/ Share on other sites More sharing options...
trq Posted November 26, 2008 Share Posted November 26, 2008 mysql_insert_id will only get the id of the last insert within the same connection, not if you made an insert some time ago. You can likely get it using something like... SELECT MAX(id) as last_id FROM tbl Link to comment https://forums.phpfreaks.com/topic/134382-insertgoto/#findComment-699609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.