What am I doing wrong?????

Lets try this here, I posted this in the General php forum earlier but thought I had it figured out and it stopped working so now I’m posting it here.
Here is my issue, I thought I had finally figured out how to upload an image to my server and store the image path in MySQL along with the rest of the form data. It did work the first few times and I was even able to get the image to display in a search page. Now here’s the problem, I did all that with a test input page and once I added all the variables and values to the PHP code for my production page it stopped working, it will still move the image to the folder but it stopped posting data to MySQL and wasn’t displaying any errors. I verified it was connecting to MySQL, the required database and table and it is but its not inputting any values in the table so I went back to square one and broke the script down to a simple form of 6 fields including an image and once again it moves the image to the folder and now it will post the form data in MySQL but it only posts the image name and not the path to the image.
So previously I noticed that the $target variable stores the path and that I didn’t have it in the INSERT function path, so I added $target into the values field and what do you know it was adding the image path to MySQL and I could display the image in a search form. Now it’s not doing that, it wont post to MySQL with $target in the values field or anywhere in the INSERT field. Someone please help this is driving me nuts!

Here is the html:

[code]

.auto-style1 { font-family: "Baskerville Old Face"; font-size: x-large; color: #FF0000; }

ADD NEW ITEM

         <p>
          Serial Number:
        </p>
        <input type="text" name="s_number">
        <p>
          Part Number:
        </p>
        <input type="text" name="p_number">
        <p>
          Model Number:
        </p>
        <input type="text" name="m_number">
        <p>
          Please Upload a Photo in gif, png, or jpg format.
        </p>
        <p>
          Photo:
        </p>
        <input type="file" name="p_image" size=35 >
        <p>
          Description:
        </p>
        <p>
          Manufactur:
        </p>
        <input type="text" name="manufacture" size=30 >
        <br>
        <br>
        <input TYPE="submit" title="Add data to the Database" value="Submit">
      </form>[/code]

And the PHP code:

[php]<?php

//This is the directory where images will be saved
$target = “uploads/images/”;
$target = $target . basename( $_FILES[‘p_image’][‘name’]);

//This gets all the other information from the form
$var1=$_POST[‘description’];
$var2=$_POST[‘p_number’];
$pic=($_FILES[‘p_image’][‘name’]);
$var3=$_POST[‘m_number’];
$var4=$_POST[‘s_number’];
$var5=$_POST[‘manufacture’];

// Connects to your Database
mysql_connect(“localhost”, “root”, “poopster”) or die(mysql_error()) ;
mysql_select_db(“test”) or die(mysql_error()) ;

//Writes the information to the database
mysql_query(“INSERT INTO test (description,p_number,p_image,m_number,s_number,manufacture)
VALUES (’$var1’, ‘$var2’, ‘$pic’, ‘$var3’, ‘$var4’, ‘$var5’)”) ;

//Writes the photo to the server
if(move_uploaded_file($_FILES[‘p_image’][‘tmp_name’], $target))
{

//Tells you if its all ok
echo "The file “. basename( $_FILES[‘p_image’][‘name’]). " has been uploaded, and your information has been added to the directory”;
}
else {

//Gives and error if its not
echo “Sorry, there was a problem uploading your file.”;
}
?>[/php]

Just a note, I removed the $target from the values field because as the code is posted the form works but doesn’t post the path. it will store all the data from the input form in MySQL it just stores the name of the image and moves it to the upload/images folder. My images folder will never change so I guess I could make a static link the the folder and just pull the image name but I want the full path stored in MySQL.

And just incase someone wants to test it out in MySQL here is the table structure to save some time:

CREATE TABLE IF NOT EXISTS `test` ( `description` varchar(40) NOT NULL, `p_number` varchar(40) NOT NULL, `m_number` varchar(40) NOT NULL, `s_number` varchar(40) NOT NULL, `manufacture` varchar(40) NOT NULL, `p_image` varchar(40) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

dang once again I figured out the issue after posting it, the $pic variable has to be replaced with the $target variable in the Values you can’t have them both or it just wont post and wont display an error either. DOH!

OK so what I thought was fixed isn’t, although the original issue of getting the path in the database is solved there appears to be something else going on with the script.
I can do 2 maybe 3 submissions to the database and that’s it, I can’t get any submissions after that to work, although every submission after that will pass the image file to the server and store it in the folder none of the data from the form is posted in MySQL and there is no error. The form acts as though it completed it’s task successfully but nothing posts in the database. Now I thought this might be an issue with my test server and xampp but I tried it on two production servers with the same results. Anybody have any ideas? is this an html issue or an issue with the script? is there some error reporting I can enable to see what the script is doing?

Out of curiosity - does any of your data have the following character sequences: ', ", --, %%, /*.

If it does - and even if it doesn’t - sanitize your data. use mysql_real_escape_string or stricter checks to reformat your data and prevent MySQL from coughing up on specific language structures.

problem fixed, my issue was one of my variables was using a reserved key word for MySQL, the word condition is reserved in MySQL and that was causing my issues. so I just renamed my table rows and my variables and now everything works as it should.

Sponsor our Newsletter | Privacy Policy | Terms of Service