Jump to content

Cannot get UPDATE to work


hadoob024

Recommended Posts

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:

[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);
            }
        }
    }
}

[/code]


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:
[code]
WHERE ofrelistings.uniqueid='$basename'
[/code]

But I know that uniqueid is stored as a UNSIGNED INT(11). Could this be why the record's not being updated properly?
Link to comment
Share on other sites

This is weird. I just noticed that if I take the resulting string from this:

[code]
$update_query = "UPDATE ofrelistings SET ofrelistings.picture='$newpath' WHERE ofrelistings.uniqueid='$basename'";

echo $update_query;
[/code]


Like the last one I ran generated:

UPDATE ofrelistings SET ofrelistings.picture='listingpics/9.jpg' WHERE ofrelistings.uniqueid='9'


When this is executed in my script, nothing gets updated. However, if I go into phpMyAdmin, and run this query through the SQL command line, the record gets updated correctly. Any thoughts?
Link to comment
Share on other sites

Cool. I tested it out and turns out that the user didn't have the SELECT priviledge and that's why it kept failing. But when I was logged into phpMyAdmin, I was logged in as Admin, so I did have the priviledge. Interesting. I didn't know you needed the SELECT priviledge when you want to UPDATE. Thanks for solving my problem!!!
Link to comment
Share on other sites

really??? that's weird, cuz when i changed that line to check for errors on the UPDATE command, i got an email with the error that said "user blahblah does not have the SELECT permission to perform this operation" or something like that. you see my UPDATE statement. what do you think caused that error message?
Link to comment
Share on other sites

yeah. isn't that weird. i still have no idea why it's happening. i just changed the logic so that i don't need to perform an UPDATE anymore. and yeah, i have no idea why it was failing because i didn't have the SELECT priviledge. i wasn't even doing anything across multiple tables. it was all within the same main table.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.