无法将图像URL发送到数据库

时间:2014-01-11 11:51:02

标签: php mysql upload

我已经设置了一个更新数据库的脚本,所有表单字段都可以工作,包括文件上传,它确实上传到我的服务器,我的问题是我希望图像的url出现在mysql表中,所以我可以在以后引用它(它与该条目相关联,但我尝试的所有内容都不想添加它。任何想法?

这是我的表格

<fieldset style="width:300px;">
<form method="post" action="./upload.php" enctype="multipart/form-data">>
Desc <input type="text" name="holdesc1"><br>
Dest <input type="text" name="holdest1"><br>
RRP <input type="text" name="rrp1"><br>
Cost Per <input type="text" name="cpe1"><br>
Entries Avail <input type="text" name="ea1"><br>
<label for="file">Select a file:</label> <input type="file" name="userfile" id="file">
<br>
<input type="submit" name="submit">
</form>
</fieldset>

这是我的上传脚本

 // Connects to your Database 
 mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error()); 
 mysql_select_db("cl52-abcdef") or die(mysql_error()); 

   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).

   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

   // We'll start handling the upload in the next step

?>


<?php
   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).

   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.



if(isset($_POST['submit']))
{
$holdesc1=mysql_real_escape_string($_POST['holdesc1']);
$holdest1=mysql_real_escape_string($_POST['holdest1']);
$rrp1=mysql_real_escape_string($_POST['rrp1']);
$cpe1=mysql_real_escape_string($_POST['cpe1']);
$ea1=mysql_real_escape_string($_POST['ea1']);
$filename=mysql_real_escape_string($_POST['filename']);

$query1=mysql_query("insert into DealOne values('','$holdesc1','$holdest1','$rrp1','$cpe1','$ea1','$filename')");
echo "insert into DealOne values('','$holdesc1','$holdest1','$rrp1','$cpe1','$ea1''$filename',)";
if($query1)
{
header("location:list.php");
}
}
?>

1 个答案:

答案 0 :(得分:0)

在代码中没有文件名字段。

$filename=mysql_real_escape_string($_POST['filename']); // this field is not in form

您有名称为userfile的输入类型文件,因此您需要使用此字段,如

$userfile = $_FILES['userfile']['name'];  // this field is in form

其次,您应该将文件上传代码移到isset($_POST)

第三次尝试使用mysqli_ *函数