On my site, a user enters in listing information (for a real estate property). My script takes in the information, INSERTs it into my db (which it's doing properly), then I get the value of the uniqueid (UNSIGNED INT(11), primary key, set to auto_increment) using mysql_insert_id(), then move the uploaded file and rename it using the value returned from mysql_insert_id() as the base of the filename, and the variable $ext as the extension. Once I move the file, I then try and UPDATE the previous entry with the new value for the picture's path. However, the UPDATE seems to run no problem, but nothing seems to get UPDATEd. I think it might have to do with how I'm checking it, but cannot tell. Here's my code:
//this variable is initialized as an emptry string because we do not yet have the //
//correct filename yet
$picpath = '';
$insert_query = "INSERT INTO ofrelistings(firstname, lastname, emailaddress, phonenumber, companyname, askingprice, listingtype, location, city, listingdescription, picture, dateadded) VALUES('$firstname', '$lastname', '$phonenumber', '$emailaddress', '$companyname', '$askingprice', '$listingtype', '$location', '$city', '$listingdescription', '$picpath', 'Date()')";
$insert_query_result = mysql_query($insert_query);
if (!$insert_query_result)
{
$problemtext = 'Error INSERT-ing information into main table in addlistingprocess.php.\r\n'.mysql_error($connresult);
trigger_error($problemtext, E_USER_ERROR);
}
//else the query passed and the record was INSERT-ed successfully
else
{
//to help with the creation of a unique file name for the uploaded pic.
$basename = strval(mysql_insert_id());
//if we're here, then the SQL string was INSERT-ed properly. send a
//message that says that the main info was added. we'll send a final
//message after we UPDATE the db with the picture file information.
$subject = 'OFRE.com New Listing Alert (part 2): '.date("F j, Y, g:i a");
$body = "The new listing was successfully added to the db. \r\n\r\nListing Number: $basename \r\nWas Picture Uploaded (1 for YES, 0 for NO): $uploadsuccess \r\n";
mail($adminrecipient, $subject, $body);
//this means that a file was uploaded successfully, so now we need to find
//the value of the primary key value for the record, so that we can create
//a unique filename for this record, using the value from mysql_insert_id()
//as the base and the value from $ext as the extension.
if ($uploadsuccess === 1)
{
//pathname for where we're moving file
$picture = 'listingpics/'.$_FILES['picture']['name'];
//moving file to location specified by $picture
if (!move_uploaded_file($_FILES['picture']['tmp_name'], $picture))
{
//just a quick alert that a listing was not moved successfully
$subject = 'OFRE.com New Listing Alert (part 3): '.date("F j, Y, g:i a");
$body = "The picture for the new listing was NOT moved successfully. \r\nListing Number: $basename \r\nPicture Path: $picture\r\n";
mail($adminrecipient, $subject, $body);
}
//else we successfully moved the picture to its new location
else
{
$newpath = 'listingpics/'.$basename.'.'.$ext;
rename ($picture, $newpath);
$basename = intval($basename);
$newpath = mysql_real_escape_string($newpath);
$update_query = "UPDATE ofrelistings SET picture='$newpath' WHERE ofrelistings.uniqueid='$basename'";
echo $update_query;
$update_result = mysql_query($update_query);
if (!$insert_query_result)
{
$problemtext = 'Error UPDATE-ing picture information in main tables in addlistingprocess.php. \r\n'.mysql_error($connresult);
trigger_error($problemtext, E_USER_ERROR);
}
//else the query was executed successfully
else
{
//just a quick alert that a listing was updated successfully with the
//path of the uploaded picture
$subject = 'OFRE.com New Listing Alert (part 3): '.date("F j, Y, g:i a");
$body = "The db was successfully updated with the picture path info. \r\nListing Number: $basename \r\nPath to Picture: $newpath \r\n";
mail($adminrecipient, $subject, $body);
}
}
}
}
However, although the UPDATE seems to execute without error, nothing gets updated. I think it has to do with how I do my comparison. Like I check the following:
WHERE ofrelistings.uniqueid='$basename'
But I know that uniqueid is stored as a UNSIGNED INT(11). Could this be why the record's not being updated properly?