Page 1 of 1

The id number won't go into the database???

Posted: Wed Jan 31, 2007 9:35 pm
by cturner
The id number from the url is not added to the database. I know that the id number is in the url. It displays nothing when I echo the id which is strange. When I echo the id before the submit button code it displays. So the submit button is the problem. Can someone please have a look at my code below and tell me why this is happening? Thanks in advance.

Code: Select all

require "config.php";
// define variables
$findphoto = $_FILES['findphoto'];
$photo = mysql_real_escape_string($_POST['photo']);
$filename = mysql_real_escape_string($_POST['filename']);
//$id = $_GET['id']; // get the id number from the url
$arrErrors = array();
// on submit
if (isset($_POST['btnsubmit'])) {
    if($_FILES['findphoto']['name'] == '') {
       $arrErrors['findphoto'] = 'You did not select a photo to upload';
    }
    if ($photo == '') {
        $arrErrors['photo'] = 'Please enter a photo name and file extension that you wish to upload for this clearing sale.';
    }
    if ($filename == '') {
        $arrErrors['filename'] = 'Please enter a file name for the photo.';
    }
    if (count($arrErrors) == 0) {
        $parent_id = $_GET['id'];
        // insert the id into the table
            $insert = "INSERT INTO `clearingsales_photos` (`parent_id`, `photos`, `filename`) VALUES ('$parent_id', '$photo', '$filename')";
        if (mysql_query ($insert)) {
            print "<strong>Photo and filename have been added to the database. Please don't forget to upload the files via ftp.</strong><br /><br />";
        } else {
            print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert.</p>";
        }
    } else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($arrErrors as $error) {
            $strError .= "<li>$error</li>";
        }
        $strError .= '</ul></div>';
    }
}

Posted: Wed Jan 31, 2007 9:43 pm
by superdezign
I'm assuming you're not to the whole upload part yet.

Code: Select all

else {
            print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert.</p>";
        }
Are you recieving this error? If so, it's a problem in your database connection.

Beyond that, I don't really see anything particularly wrong in the php. If you're problem is beyond that, try making your form's action="" include the id.

Posted: Wed Jan 31, 2007 9:47 pm
by cturner
I am receiving no errors at all which is frustrating.

Posted: Wed Jan 31, 2007 9:50 pm
by superdezign
Well when I search for errors, I do this:

In each level of my if statements, I simply put

Code: Select all

echo "So far, so good";
Refresh the page and if I see it, move it to another if statement and try again. It's actually a fairly quick process. Find out where your script isn't going. If it's going everywhere, then the error isn't in the conditions. Just a good way to make sure.

Posted: Thu Feb 01, 2007 12:06 am
by gavin1996
$insert = "INSERT INTO `clearingsales_photos` (`parent_id`, `photos`, `filename`) VALUES ('$parent_id', '$photo', '$filename')";


echo $insert; <---what's the Result?

Posted: Thu Feb 01, 2007 12:09 am
by superdezign
Or more importantly, is anything going in? Or is it JUST not the id not being inserted? I'm thinking maybe the problem is your table. Is your id field auto_increment or enum by any chance?

Posted: Thu Feb 01, 2007 1:37 pm
by WorldCom
Just a guess.

It looks as though your using:

Code: Select all

$parent_id = $_GET['id'];
Inside your if statement (isset($_POST['btnsubmit'])).

Would you not need to have it:

Code: Select all

$parent_id = $_POST['id'];
And make sure you have a the 'id' variable in your form.