Forum Moderators: coopster
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
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.
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
Best regards
Michal Cibor
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