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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

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

Post 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>';
    }
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I am receiving no errors at all which is frustrating.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
gavin1996
Forum Newbie
Posts: 22
Joined: Tue Jan 30, 2007 8:30 pm

Post by gavin1996 »

$insert = "INSERT INTO `clearingsales_photos` (`parent_id`, `photos`, `filename`) VALUES ('$parent_id', '$photo', '$filename')";


echo $insert; <---what's the Result?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Post 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.
Post Reply