通过单个PHP表单将数据和文件上载到MySQL数据库

时间:2014-07-08 23:17:38

标签: php html mysql database uploading

我在HTML表单和文件上传器中有一些输入类型。但我无法在MySQL数据库中同时上传数据和文件。每次空白条目填入数据库......

HTML表单

<form method="post" action="upload.php" enctype="multipart/form-data">
    <p>
        <label>Name:</label>
        <input type="text" name="name" size="40">
    </p>
    <p>
        <label>Email:</label>
        <input type="email" name="email">
    </p>
    <p>
        <input type="radio" name="gen" value="Male">Male
        <input type="radio" name="gen" value="Female">Female</p>
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload:
    <br>
    <input type="file" name="File" size="40">
    <p>
        <input type="submit" name="submit" value="submit">
</form>

PHP代码

if(isset($_POST['name']) && isset($_POST['email']) &&
        isset($_POST['gen']) && $_FILES['File']['size'] > 0) {
    $name=get_POST('name');
    $email=get_POST('email');
    $gender=get_POST('gen');
    $filename = $_FILES['File']['name'];
    $tmpname  = $_FILES['File']['tmp_name'];
    $filesize = $_FILES['File']['size'];
    $filetype = $_FILES['File']['type'];
    $fp = fopen($tmpname, 'r');
    $file = fread($fp, filesize($tmpname));
    $file = addslashes($file);
    fclose($fp);

    if(!get_magic_quotes_gpc()) {
        $filename = addslashes($filename);
    }
    $query = "
        INSERT INTO uploadform
        (Name, Email, Gender, Filename, Filetype, Filesize, File) VALUES
        ('$name', '$email', '$gender', '$filename', '$filetype', '$filesize', '$file');";

    mysql_query($query) or die('Error, query failed');
}

在数据库中,文件的数据类型为MEDIUMBLOB

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案......!只需检查变量......我使用过其他数据库和示例(常识)......

<?php 
//This is the directory where images will be saved 
$target = "images/"; 
    if(!is_dir($target)) mkdir($target);
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$name=$_POST['username']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$fname=($_FILES['photo']['name']); 
$tmpName  = $_FILES['photo']['tmp_name'];
$fileSize = $_FILES['photo']['size'];
$fileType = $_FILES['photo']['type'];



//process the file
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc()){
$fname = addslashes($fname);}

// Connects to your Database 
require_once 'login.php';
$db_server=mysql_connect($db_hostname,$db_username,$db_password);

if(!$db_server) die("Unable to connect to MySQL" .mysql_error());

mysql_select_db($db_database,$db_server)
or die("Unable to connect to database" .mysql_error()); 

//Writes the information to the database 
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$fname','$fileType','$fileSize','$content')") ; 

 //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']['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."; 
    } 
     ?> 
      <form enctype="multipart/form-data" action="up.php" method="POST"> 
      Name: <input type="text" name="username"><br> 
      E-mail: <input type="text" name = "email"><br> 
      Phone: <input type="text" name = "phone"><br> 
      <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
      Photo: <input type="file" name="photo"><br> 
      <input type="submit" value="Add"> 
      </form>