Id number displays 0 and not 1

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Id number displays 0 and not 1

Post by cturner »

When I test the following code it displays 0 in the url and not 1. Can someone please have a look at my code and tell me how I can solve this problem? Thanks in advance.

Code: Select all

// variables defined before this
require "config.php";
$update = "UPDATE clearingsales SET propertyname = '$propertyname', time = '$time', accountname = '$accountname', comments = '$comments', select1 = '$select1', txt1 = '$txt1', select2 = '$select2', txt2 = '$txt2', select3 = '$select3', txt3 = '$txt3', select4 = '$select4', txt4 = '$txt4', select5 = '$select5', txt5 = '$txt5', select6 = '$select6', txt6 = '$txt6', select7 = '$select7', txt7 = '$txt7', select8 = '$select8', txt8 = '$txt8', select9 = '$select9', txt9 = '$txt9', select10 = '$select10', txt10 = '$txt10', select11 = '$select11', txt11 = '$txt11' WHERE propertyname = '$propertyname'";
if (mysql_query ($update)) {
		$query = mysql_query("SELECT id, propertyname FROM `clearingsales` WHERE `propertyname` = '$propertyname'") or die ("Could not query because: ".mysql_error());
		$row = mysql_fetch_assoc($query);
		$insertID = mysql_insert_id();
		header ("Location: clearing_sale_edited.php?id=".$insertID);
	} else {
		print "<p>Could not update the entry because: <b>" . mysql_error() . "</b>. The query was $update.</p>";
	}
mysql_close();
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de3.php.net/function.mysql_insert_id wrote:Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
Whre's the INSERT statement?
What are you trying to achieve?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

instead of:

Code: Select all

$insertID = mysql_insert_id();
try:

Code: Select all

$insertID = $row['id'];
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I have changed my code superdezign to what you said. Now I am getting no id number in the url.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Use mysql_affected_rows to check wether UPDATE really changed a record.
use false!==$row to test wether the SELECT statement actually fetched a record.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I have now changed my code to:

Code: Select all

$update = "UPDATE clearingsales SET propertyname = '$propertyname', time = '$time', accountname = '$accountname', comments = '$comments', select1 = '$select1', txt1 = '$txt1', select2 = '$select2', txt2 = '$txt2', select3 = '$select3', txt3 = '$txt3', select4 = '$select4', txt4 = '$txt4', select5 = '$select5', txt5 = '$txt5', select6 = '$select6', txt6 = '$txt6', select7 = '$select7', txt7 = '$txt7', select8 = '$select8', txt8 = '$txt8', select9 = '$select9', txt9 = '$txt9', select10 = '$select10', txt10 = '$txt10', select11 = '$select11', txt11 = '$txt11' WHERE propertyname = '$propertyname'";
if (mysql_query ($update)) {
		$id = mysql_real_escape_string($_GET['id']);
		header ("Location: clearing_sale_edited.php?id=".$id);
	} else {
		print "<p>Could not update the entry because: <b>" . mysql_error() . "</b>. The query was $update.</p>";
	}
I am still getting no id number in the url.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Really? That's odd. In that case, I'm stumped. Something in your code must not be happening correctly. Either your table update isn't happeneing, or your select isn't happening.

Or your table column "id" is empty. I doubt it's that bad though.

Edit: I think volka's leading ou the right way, I'm going to sleep now.
Last edited by superdezign on Sat Jan 27, 2007 12:27 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a) why mysql_real_escape_string when you want to use the result in an url (not mysql) ?
b) is there a _GET['id']? Use print_r($_GET); to check that.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I have placed print_r($_GET); into my code and it displays Array ( [id] => 1 ).
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

error_reporting(E_ALL); ini_set('display_errors', true);
$update = "UPDATE clearingsales SET propertyname = '$propertyname', time = '$time', accountname = '$accountname', comments = '$comments', select1 = '$select1', txt1 = '$txt1', select2 = '$select2', txt2 = '$txt2', select3 = '$select3', txt3 = '$txt3', select4 = '$select4', txt4 = '$txt4', select5 = '$select5', txt5 = '$txt5', select6 = '$select6', txt6 = '$txt6', select7 = '$select7', txt7 = '$txt7', select8 = '$select8', txt8 = '$txt8', select9 = '$select9', txt9 = '$txt9', select10 = '$select10', txt10 = '$txt10', select11 = '$select11', txt11 = '$txt11' WHERE propertyname = '$propertyname'";
if (mysql_query ($update)) {
	$header="Location: clearing_sale_edited.php?id=".$_GET['id'];
	echo $header;
	// header($header);
}
else {
	print "<p>Could not update the entry because: <b>" . mysql_error() . "</b>. The query was $update.</p>";
}
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I have my the changes to my code volka. Now I am getting Undefined index. So what does that mean?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

undefined index 'id'?
Then I do not know how this fits
cturner wrote:I have placed print_r($_GET); into my code and it displays Array ( [id] => 1 ).
try

Code: Select all

error_reporting(E_ALL); ini_set('display_errors', true);
$update = "UPDATE clearingsales SET propertyname = '$propertyname', time = '$time', accountname = '$accountname', comments = '$comments', select1 = '$select1', txt1 = '$txt1', select2 = '$select2', txt2 = '$txt2', select3 = '$select3', txt3 = '$txt3', select4 = '$select4', txt4 = '$txt4', select5 = '$select5', txt5 = '$txt5', select6 = '$select6', txt6 = '$txt6', select7 = '$select7', txt7 = '$txt7', select8 = '$select8', txt8 = '$txt8', select9 = '$select9', txt9 = '$txt9', select10 = '$select10', txt10 = '$txt10', select11 = '$select11', txt11 = '$txt11' WHERE propertyname = '$propertyname'";
if (mysql_query ($update)) {
	echo '<pre>'; var_export($_GET); echo "</pre>\n";
	$header="Location: clearing_sale_edited.php?id=".$_GET['id'];
	echo $header;
	// header($header);
}
else {
	print "<p>Could not update the entry because: <b>" . mysql_error() . "</b>. The query was $update.</p>";
}
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Undefined index is actually for propertyname to other11 not id. Sorry for not saying that earlier. What does undefined index mean?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$arr = array(
		'abc' => '123'
	);
	
$e = $arr['abc']; // no problem, there is an index 'abc' in $arr
$e = $arr['xyz']; // undefined index:   xyz
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Okay I understand that now. But I can't find a reason why code is displaying those errors. Could I please post my code here and someone find the problems? I am about to tear my hair if I don't fix those errors soon. This is bugging me.
Post Reply