id problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

id problem

Post by rsmarsha »

Code: Select all

$enterorder = "INSERT INTO orders_main (orderno,user_id,username,dateadded,status,archive,price,exvat,payment,q,discount,total,totalexvat,deldet,refer,l_model,config) VALUES ('','$create_id','$mail','$datejoined','$type','','$_POST[price]','$_POST[exvat2]','Finance','$_POST[quantity]','$_POST[discount]','$_POST[final]','$_POST[extotal]','$_POST[deldet]','$refer','','')";
$enterq = mysql_query($enterorder, $db_conn) or die("Query enter order failed".mysql_error());
The above code enters an id using the variable $create_id, for some reason it enters 32767 all the time. If i change the variable name to an undefined variable it enters 0 as it should, but no matter what i call the variable when defining it, the result is the same number. What makes this even more confusing is that the line above this code echo's the variable onto the page and it shows the correct id.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

show us the code for generating the id. Is the id a string because I see you using quotes around it?
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

The id is a number, this number is pulled from another table. If old user from user table, new user same table using mysql_insert_id().

The id is generated correctly as if i place an echo for the id on the line above the code i posted, it echo's the correct id.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Re: id problem

Post by raghavan20 »

rsmarsha wrote:

Code: Select all

$enterorder = "INSERT INTO orders_main (orderno,user_id,username,dateadded,status,archive,price,exvat,payment,q,discount,total,totalexvat,deldet,refer,l_model,config) VALUES ('','$create_id','$mail','$datejoined','$type','','$_POST[price]','$_POST[exvat2]','Finance','$_POST[quantity]','$_POST[discount]','$_POST[final]','$_POST[extotal]','$_POST[deldet]','$refer','','')";
$enterq = mysql_query($enterorder, $db_conn) or die("Query enter order failed".mysql_error());
I just noted that you have forgot to put $ before user_id which assumes it as constant. Please use error_reporting(E_ALL) during development of an application.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

A $ before user_id?

So i'd have :

Code: Select all

$enterorder = "INSERT INTO orders_main (orderno,$user_id,username,dateadded
That creates errors, as the user_id is a database field.

I've also tried echo'ing every posted variable and there is nothing with the number it's entering. Also as i've said the line before echo's the variable which i'm trying to enter and it shows correct. Somehow between one line and the next the variable i'm entering changes.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

ok even tried :

Code: Select all

$findid = "SELECT id FROM users WHERE username='$mail'";
$idq = mysql_query($checkuser, $db_conn) or die("Query $findid failed".mysql_error());
$idrow = mysql_fetch_assoc($idq);
$userid = $idrow['id'];

echo ''.$userid.'';

$type = 'Awaiting credit check';
$enterorder = "INSERT INTO orders_main (orderno,user_id,username,dateadded,status,archive,price,exvat,payment,q,discount,total,totalexvat,deldet,refer,l_model,config) VALUES ('','$userid','$mail','$datejoined','$type','','$_POST[price]','$_POST[exvat2]','Finance','$_POST[quantity]','$_POST[discount]','$_POST[final]','$_POST[extotal]','$_POST[deldet]','$refer','','')";
	$enterq = mysql_query($enterorder, $db_conn) or die("Query enter order failed".mysql_error());
It still enters in the number even though the id is defined right above it.

Also if i remove the first query and define user id as 1 instead of idrow['id'] it works.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

$findid = "SELECT id FROM users WHERE username='$mail'"; 
$idq = mysql_query($checkuser, $db_conn) or die("Query $findid failed".mysql_error());
you should be using mysql_query($findid, $db_conn) but I see you using mysql_query($checkuser, $db_conn) :roll:
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

changed that and it still enters 32767. :(
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

$findid = "SELECT `id` FROM `users` WHERE `username` = '$mail' "; 
$idq = mysql_query($findid, $db_conn) or die("Query $findid failed".mysql_error()); 
echo "<br />Query: "$idq;
$idrow = mysql_fetch_assoc($idq); 
if (is_resource($idrow)){
	if (mysql_num_rows($idrow) > 0){
		echo "<br /> Rows returned: ". $mysql_num_rows($idrow);
		echo "<br />Before assignment - user id: ".$userid;
		$userid = $idrow['id']; 
		echo "<br /> After assignment - user id".$userid; 

		$type = 'Awaiting credit check'; 
		$enterorder = "INSERT INTO orders_main (orderno,user_id,username,dateadded,status,archive,price,exvat,payment,q,discount,total,totalexvat,deldet,refer,l_model,config) 
VALUES ('','$userid','$mail',$datejoined','$type','','$_POST[price]','$_POST[exvat2]','Finance','$_POST[quantity]','$_POST[discount]','$_POST[final]','$_POST[extotal]','$_POST[deldet]','$refer','','')"; 
		echo "<br > Insert Query: ".$enterorder;
		mysql_query($enterorder, $db_conn) or die("Query enter order failed".mysql_error()); 
		echo "Rows Affected: ".mysql_affected_rows();
	}
}
Run the above and post here the output to the browser....
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

I got :

Query: Resource id #14
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

findid = "SELECT `id` FROM `users` WHERE `username` = '$mail' "; 
$idq = mysql_query($findid, $db_conn) or die("Query $findid failed".mysql_error()); 
echo "<br />Result: "$idq; 
$idrow = mysql_fetch_assoc($idq); 
if (is_resource($idrow)){ 
	echo "<br /> Rows returned: ". $mysql_num_rows($idrow); 
    if (mysql_num_rows($idrow) > 0){ 
        
        echo "<br />Before assignment - user id: ".$userid; 
        $userid = $idrow['id']; 
        echo "<br /> After assignment - user id".$userid; 

        $type = 'Awaiting credit check'; 
        $enterorder = "INSERT INTO orders_main (orderno,user_id,username,dateadded,status,archive,price,exvat,payment,q,discount,total,totalexvat,deldet,refer,l_model,config) 
VALUES ('','$userid','$mail',$datejoined','$type','','$_POST[price]','$_POST[exvat2]','Finance','$_POST[quantity]','$_POST[discount]','$_POST[final]','$_POST[extotal]','$_POST[deldet]','$refer','','')"; 
        echo "<br > Insert Query: ".$enterorder; 
        mysql_query($enterorder, $db_conn) or die("Query enter order failed".mysql_error()); 
        echo "Rows Affected: ".mysql_affected_rows(); 
    } 
}
It looks like it is not returning any row for the id. I forgot to put the echo statement for number of rows outside the if block...run this code again and give me the output...thanks
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Result: Resource id #14

Is the lastest output.

Btw thanks for all this help. :) Hopefully will get it sorted.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

any ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

rsmarsha wrote:Result: Resource id #14

Is the lastest output.

Btw thanks for all this help. :) Hopefully will get it sorted.
Tell us ALL the output :wink:
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

that is all the output.
Post Reply