m118 Posted January 27, 2011 Share Posted January 27, 2011 I get mysql_insert_id problem. It returns 0. I do not know how to fix it. Please tell me. Thank you very much. <?php session_id(); session_start();?> <!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=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php include("connection.php"); $subtotal=$_POST['subtotal']; $tax=$_POST['tax']; $total=$_POST['total']; $today=date("Y-m-d"); $email=$_SESSION['email']; $od=mysql_insert_id(); $number=$_POST['number']; $name=$_POST['name']; $month=$_POST['month']; $year=$_POST['year']; $code=$_POST['code']; $method=$_POST['type']; $add1="select * from customer where email='$email'"; $add2=mysql_query($add1); $add3=mysql_fetch_array($add2); extract($add3); $qiu1="INSERT INTO test (oid) VALUES ('$email')"; $qiuzhen=mysql_query($qiu1); $orderid=mysql_insert_id(); $query44="INSERT INTO income (subtotal,tax,total,time,email,address,credit,name,month,year,code,method,custid) VALUES ('$subtotal','$tax','$total','$today','$email','$add1',$number,'$name','$month','$year','$code','$method','$orderid')"; $result=mysql_query($query44); echo "successful!"; $orderid=mysql_insert_id(); echo "$email"; echo "$total"; $sessid=session_id(); $qu="select * from carttemp where sess='$sessid'"; $result=mysql_query($qu); while($w=mysql_fetch_array($result)){ extract($w); $query7="INSERT INTO try (prodnum,quan,custnum) VALUES ('$prodnum','$quan','$custid')"; $iii=mysql_query($query7) or (mysql_error()); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/ Share on other sites More sharing options...
Hangwire Posted January 27, 2011 Share Posted January 27, 2011 Be a little more specific, what error does it exactly output? Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166254 Share on other sites More sharing options...
m118 Posted January 27, 2011 Author Share Posted January 27, 2011 There is no any error message. I checked the database, the data is zero. Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166259 Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2011 Share Posted January 27, 2011 Is anything at all being inserted? Does the table have an autoincrement index field? Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166263 Share on other sites More sharing options...
m118 Posted January 27, 2011 Author Share Posted January 27, 2011 Yes. The table has one auto_increment. Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166273 Share on other sites More sharing options...
l4nc3r Posted January 27, 2011 Share Posted January 27, 2011 According to the manual: Return Values The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established. So the previous query did not generate an AUTO_INCREMENT, so either there's a mysql_error(), or the test table does not have an AUTO_INCREMENT field. Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166274 Share on other sites More sharing options...
jcbones Posted January 27, 2011 Share Posted January 27, 2011 You have two $orderid both are populated by mysql_insert_id, which do you want. Also, give us a Table Structure dump of the tables in question. Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166275 Share on other sites More sharing options...
m118 Posted January 27, 2011 Author Share Posted January 27, 2011 I want two custnum is same, I use it. By the way , Can you tell me how to create a unique number ? It can join the database. The two table code CREATE TABLE `income` ( `id` int(6) NOT NULL auto_increment, `subtotal` decimal(7,2) default NULL, `tax` decimal(7,2) default NULL, `total` decimal(7,2) default NULL, `time` date NOT NULL, `email` varchar(50) NOT NULL, `address` varchar(60) NOT NULL, `credit` varchar(50) NOT NULL, `name` char(20) NOT NULL, `month` char(15) NOT NULL, `year` char(10) NOT NULL, `code` char(10) NOT NULL, `method` char(20) NOT NULL, `custid` int(6) NOT NULL, PRIMARY KEY (`id`) ) CREATE TABLE `try` ( `id` int(6) NOT NULL auto_increment, `prodnum` varchar(20) NOT NULL, `quan` int(3) NOT NULL, `custnum` int(6) NOT NULL, PRIMARY KEY (`id`) ) Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166279 Share on other sites More sharing options...
jcbones Posted January 27, 2011 Share Posted January 27, 2011 Ok, change: $result=mysql_query($query44); echo "successful!"; To: if($result=mysql_query($query44)) { echo "successful!"; } else { echo mysql_error() . ' <br /> in ' . $query44; } Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166288 Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2011 Share Posted January 27, 2011 If mysql_insert_id() returns 0, your query is failing. You need to add logic to check for successful execution and to make sure the query actually inserted a record, and if either of those don't happen, report the error. Quote Link to comment https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/#findComment-1166290 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.