Forum Moderators: coopster

Message Too Old, No Replies

Just can't understand it - doesn't update

Update doesnt update it adds

         

Moonie49

11:49 am on Sep 22, 2005 (gmt 0)

10+ Year Member



PHP newbie - but enjoying it. Stuck with this one and would appreciate any advise.

connect to database fine - can add items fine - can delete fine - but when call record to edit, it adds additional record and does not update record called.

Code is:

// get the variables from the URL request string
$id = $_REQUEST['id'];
$stocknumber = $_REQUEST['stocknumber'];
$category = $_REQUEST['category'];
$description = $_REQUEST['description'];
$height = $_REQUEST['height'];
$width = $_REQUEST['width'];
$price = $_REQUEST['price'];

// if $id is not found, add a new entry, otherwise update the old entry
if( $id )
{
$query = "UPDATE `items` SET `stocknumber`='$stocknumber', `category`='$category', `description`='$description', `height`='$height', `width`='$width',`price`='$price' WHERE `id`='$id'";
}
else
{
$query = "INSERT INTO `items` ( `stocknumber`,`category`,`description`,`height`, `width`, `price` ) VALUES ( '$stocknumber','$category','$description', '$height', '$width', '$price') ";
}

// save the info to the database
$results = mysql_query( $query );

Everything great but doesnt update - any ideas appreciated

jatar_k

3:18 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Moonie49,

it looks like it is not hitting the update part of the if statement.

I would echo the query at the end instead of executing it to see if that is the case. If it echo's an INSERT query when it should be updating then look to your $id var as something is wrong with the test or how you are passing the var.

mcibor

9:06 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Moreover try not to use the $_REQUEST. Use either $_GET or $_POST instead.

The problem lies explicitly in the $id fields not posted properly.
I found, that the best method for debugging logic of the script is temporary echoing of the unsure part:

Just for debugging reasons you may try this code:

echo "Id: $id<br>";
if( $id )
{
echo "The id is true";
$query = "UPDATE `items` SET `stocknumber`='$stocknumber', `category`='$category', `description`='$description', `height`='$height', `width`='$width',`price`='$price' WHERE `id`='$id'";
}
else
{
echo "The id is false";
$query = "INSERT INTO `items` ( `stocknumber`,`category`,`description`,`height`, `width`, `price` ) VALUES ( '$stocknumber','$category','$description', '$height', '$width', '$price') ";
}

echo "<br>Query: $query<br>;
// save the info to the database
//$results = mysql_query( $query ); for debugging reasons you don't need this part


Hope this helps you now and in the future

Best regards
Michal Cibor

Moonie49

9:21 am on Sep 24, 2005 (gmt 0)

10+ Year Member



Wow - two quick replies. Thanks guys.

Idid as you suggested - echoing the id proved as I (and you) thought that the id was being passed correctly.

After numerous code changes and total confusion at times :-) I lost the id totally. on putting it all back I was amazed to find it worked!

once again, many thanks.

Appreciated