joshgarrod Posted January 29, 2009 Share Posted January 29, 2009 I have an image uploader and submitting form, the form uploads the image and stores it in a folder called "uploads", then I need to find the name of the image and store it in the database field "image" so that it knows where to source the image from when I process the data. All it does at the mo is puts "uploads/" into the databse. Can any body help please? PHP: <?php $idir = "uploads/"; // Path To Images Directory if (isset ($_FILES['fupload'])){ //upload the image to tmp directory $url = $_FILES['fupload']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload']['tmp_name'], "$idir" . $_FILES['fupload']['name']); // Move Image From Temporary Location To Permanent Location if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location print '' .$url . ' Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image } } } error_reporting (E_ALL ^ E_NOTICE); $usr = "usr"; $pwd = "pass"; $db = "fb"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } if ($_POST['submit']) { $title = mysql_real_escape_string($_POST['title']); $descr = mysql_real_escape_string($_POST['descr']); $partno = mysql_real_escape_string($_POST['partno']); $price = mysql_real_escape_string($_POST['price']); $avail = mysql_real_escape_string($_POST['avail']); $image = mysql_real_escape_string($_POST['image']); $SQL = "INSERT INTO spares"; $SQL .= " (title, descr, partno, price, avail, image) VALUES "; $SQL .= " ('$title','$descr','$partno','$price', '$avail', '$image') "; $result = mysql_db_query($db,$SQL,$cid); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P><B>Congratulations... you have successfully added a new part to the stock list - $image_name</B></P>\n"); } ?> HTML: <input name="image" type="hidden" value="uploads/<?php echo $url; ?>" /> Link to comment https://forums.phpfreaks.com/topic/142967-solved-problem-with-passing-variable-in-hidden-form-field-to-database/ Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 get rid of the hidden input field...and change this line: $image = mysql_real_escape_string($_POST['image']); to $image = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']); Link to comment https://forums.phpfreaks.com/topic/142967-solved-problem-with-passing-variable-in-hidden-form-field-to-database/#findComment-749627 Share on other sites More sharing options...
joshgarrod Posted January 29, 2009 Author Share Posted January 29, 2009 ok, I will try that thank you Link to comment https://forums.phpfreaks.com/topic/142967-solved-problem-with-passing-variable-in-hidden-form-field-to-database/#findComment-749638 Share on other sites More sharing options...
joshgarrod Posted January 29, 2009 Author Share Posted January 29, 2009 thanks v much, works now Link to comment https://forums.phpfreaks.com/topic/142967-solved-problem-with-passing-variable-in-hidden-form-field-to-database/#findComment-749649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.