Jump to content

posting image path to MySQL


Vegas1
Go to solution Solved by mac_gyver,

Recommended Posts

So here is my problem, I'm developing this for my business and what I'm doing is in an inventory form I display the image of the item in inventory, when I add a new item to the inventory the image is uploaded to the server stored in a folder and  ideally the path to the image is stored in MySQL, well I had it working with the current script I'm using, I was even able to display the image in a search page. Put now it doesn't work, the image is still uploaded to the server and put in the correct folder but the path won't post in MySQL, as a matter of fact nothing posts to the database at all. Originally I was able to get the script to upload the image and move it to the permanent folder but it was only posting the form content and the name of the image not the path. I realized that the $target variable needed to be included in the INSERT function so I added $target to the values and it was working but now it's stopped, all  I get is a confirmation that the image was posted and I can see it's in the folder but nothing gets posted to the database. I'm not sure if this is a problem with the code or if  I have some issues with xampp so I broke the script down into a basic 6 input page and tried it with the $target variable not in the INSERT field and it will post the data but only the image name not the path but it does move the image to the correct folder. what am I doing wrong?

 

Note that I never get any type of errors from my PHP script at all, every time I get a confirmation that the file with the image name has been uploaded and the image does in fact upload to the correct folder but I get no errors at all unless I force an error by wrong password or such or create a parse error or something along that lines so I know the script is working.

here is my html:


<head>
<style type="text/css">
.auto-style1 {
	font-family: "Baskerville Old Face";
	font-size: x-large;
	color: #FF0000;
}
</style>
</head>

<span class="auto-style1">ADD NEW ITEM</span><form method="post" action="process_original.php" enctype="multipart/form-data">
     
             <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>
<textarea cols="35" name="description" style="height: 35px">
</textarea>
            
            <p>
              Manufactur:
            </p>
            <input type="text" name="manufacture" size=30 >
            <br>
            <br>
            <input TYPE="submit" title="Add data to the Database" value="Submit">
          </form>

and here is my PHP code:

<?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", "xxxxx") 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))<----I'm pretty sure this needs to be in the values but how? 
{

//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.";
}
?>
Edited by Vegas1
Link to comment
Share on other sites

Never mind I figured it out right after posting this, the $pic variable has to be removed from the values field and $target put in it's place and it will post the image path in the image field of MySQL otherwise with both of them in the value they bump heads and nothing gets posted to the db and no error is displayed either.

Link to comment
Share on other sites

Ok so I though the problem was resolved, well the original of the image path being stored in MySQL is solved but after doing 2 submissions with the add_new.html form it just stops working. No errors whatsoever, the script continues to process the input form, it will take the image and move it to the correct folder on the server and post a success reply saying the name of the image and that it's been uploaded but none of the data is posted in MySQL, nothing, not the image path, image name nor any of the other data from the form. it just stops posting to MySQL. I thought this may be a problem with my test server and xampp so I tried it on 2 production servers with the same results. 

So, I'm stumped, is it a problem with the html form or is it in the PHP code?

Edited by Vegas1
Link to comment
Share on other sites

you need to use mysql_error() to find out why your query is failing. for debugging only, add - echo mysql_error(); on the next line after your mysql_query() line.

 


 

it's likely because your data contains special sql characters that are breaking the sql syntax and you are not escaping (see the mysql_real_escape_string() function) the string data being put into the query or the programming gods are upset because you are using non-helpful variable names like $var1, $var2, $var3 that don't indicate the purpose of the variable.

Link to comment
Share on other sites

So I added the echo MySQL_error(); and here is what it outputs:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, availability, reason, notes, warranty_date, purchase_date, location, ' at line 1The file ampwhite_cat3.jpg has been uploaded, and your information has been added to the directory

 

I've checked the 7 listed values and there is no difference them and the other 9 values. other than the other values have an underscore, so part_number, serial_number and so on? do I need to keep using that format in my values?

Link to comment
Share on other sites

Thanks for your help mac_gyver, I renamed all my colums and changed my variable names as well and now it all works. Thanks for the link to the key words, I was wondering if I was using something that was reserved.

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.