Jump to content

mysql_insert_id problem


m118

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/225895-mysql_insert_id-problem/
Share on other sites

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.

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`)

)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.