Re: howto get PK id after INSERT??
| From: | John W. Holmes | Date: | Tue, 20 Jul 2004 14:14:23 +0000 |
| Subject: | Re: howto get PK id after INSERT?? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-35410@lists.php.net to get a copy of this message | ||
Vincent Jordan wrote:
I think I got it correct got the most part. I am having a problem with the urlencode function I believe. On page CustomerAddNew1.php ( page than handles form data ) im using: $last_id = mysql_query("SELECT LAST_INSERT_ID() from customerinfo");$last_id is now a Result Resource, not the value you're after. You must use mysql_result() or any of the mysql_fetch_*() functions to retrieve the value... $lastid = mysql_result($last_id,0);
$last_id = urlencode ($last_id);No need to urlencode an integer.
header("Location: UserMain.php?custid='$last_id'");You don't put quotes around values in the URL.
and on the UserMain.php page I am using this to return the data: $cid = "$last_id";You called it "custid" in the URL, not "last_id". All you need here is $cid = $cust_id although that's a waste of code. What you really want is $cid = (int)$_GET['cust_id']; so that now you know $cid is an integer and you're not opening yourself wide open to SQL injection attacks later.
ini_set('display_errors', 1); error_reporting(E_ALL &~ E_NOTICE); $connect = mysql_connect("") or die ("unable to connect to database" . mysql_error() . ""); $select = mysql_select_db("") or die ("unable to connect to database" . mysql_error() . "SPDATA"); $result = mysql_query("select * from customerinfo where custid='$cid'") or dieThe "custid" column is an integer, right? Why are you passing it a string by putting quotes around $cid?
(mysql_error("Unable to query database")); while ($row = mysql_fetch_array($result)) { $firstname = $row['firstname']; $lastname = $row['lastname']; <snip>This whole process of $var = $row['var'], $var2 = $row['var2'] is a waste of resources. Why do you need to rename the variable? You already have $row['firstname'] as a variable, just use it. If you don't understand how to use an array when printing a string, then check the manual. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com