Dicko_md Posted February 3, 2013 Share Posted February 3, 2013 Hi I dont think that it will be difficult for some of you on here to spot my flaw but I am trying to add a record and a image to a folder and register the name of the file along with the full path to the image in the mysql record. The issue I am having is that the image is uploading OK but nothing from the form is uploading into the database. My columns are named as ID, Display Name, Model Type, Fix Type, Description, Price, image Also...where would I put the http://www.XXXX.co.uk/images/catalog in my addrecord.php script so that it inserts it along with the file name of the file in the image column, so I can call this image later ? This is my form <form method="post" action="addrecord.php" enctype="multipart/form-data"> <p> ID </p> <input type="text" name="nameid"/> <p> <p> Display Part Name </p> <input type="text" name="namepart"/> <p> iPhone Model </p> <input type="text" name="nameiphone"/> <p> Fix Type </p> <input type="text" name="namefix"/> <p> Description </p> <input type="text" name="namedescription"/> <p> Price </p> <input type="text" name="nameprice"/> <p> Image: </p> <input type="file" name="photo"> <br/> <br/> <input TYPE="submit" name="upload" title="Insert Record" value="Insert Record"/> </form> This is the add record.php file My code is <?php //This is the directory where images will be saved $target = "/home/XXXX/public_html/images/catalog/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $ID=$_POST['nameid']; $displayname=$_POST['namepart']; $iphone=$_POST['nameiphone']; $fix=$_POST['namefix']; $description=$_POST['namedescription']; $price=$_POST['nameprice']; $pic=($_FILES['photo']['name']); // Connects to your Database mysql_connect("localhost", "XXXXXX", "iphone") or die(mysql_error()) ; mysql_select_db("XXXXX_catalog") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO catrep (nameid,namepart,nameiphone,namefix,namedescription,nameprice,photo) VALUES ('$ID','$displayname', '$iphone', '$fix', '$description','$price','$pic')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['photo']['displayname']). " 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."; } ?> Thanks in advance Martyn Quote Link to comment https://forums.phpfreaks.com/topic/273995-add-record-and-image-with-php/ Share on other sites More sharing options...
Love2c0de Posted February 3, 2013 Share Posted February 3, 2013 (edited) Not sure what the problem could be to be honest but give this a go: //Writes the information to the database $result = mysql_query("INSERT INTO catrep (nameid,namepart,nameiphone,namefix,namedescription,nameprice,photo) VALUES ('{$ID}','{$displayname}', '{$iphone}', '{$fix}', '{$description}','{$price}','{$pic}')") ; //Writes the photo to the server if($result != FALSE && move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['photo']['displayname']). " 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."; } Just overwrite everything starting from your query to the end of the script with this. Also, try putting a var_dump($_POST); statement at the top of your script and see what that reveals. Although I don't see any problem with the form input names and your $_POST data retrieving. With your second question, create anothe field in your table of varchar type. Then just save $_FILES['photo']['name'] to a variable (like you have done with your $_POST data) and then amend the query. Hope this helps, Regards, L2c. Edited February 3, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/273995-add-record-and-image-with-php/#findComment-1409948 Share on other sites More sharing options...
Love2c0de Posted February 3, 2013 Share Posted February 3, 2013 With your second question, create anothe field in your table of varchar type. Then just save $_FILES['photo']['name'] to a variable (like you have done with your $_POST data) and then amend the query. Then to use the files later, you can st up a variable holding the path, then just loop though your images from the database like so: //do query to get data from that table.....save it into variable $query $directory = "/home/XXXX/public_html/images/catalog/"; while($row = mysql_fetch_array($query)) { echo "<img src='{$directory}{$row['image_field']}' alt='altText' />"; } Hope this helps, Regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/273995-add-record-and-image-with-php/#findComment-1409952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.