drlh197610
drlh197610
2014-11-29 11:16

PHP将文件和文本从多个文本字段上传到mysql

已采纳

I'm making a form with 5 text fields and 1 file field. When I tried having 2 text fields it works but if there's more,nothing works.The image successfully uploads to the folder. here's what i have so far. I'm still a noob and i'm in the stage where i gather codes that work so i'm sorry if these are outdated,messy, or something.

<?php
if(isset($_FILES['filename'])){
$errors = array();
$file_name = $_FILES['filename']['name'];
$file_size =$_FILES['filename']['size'];
$file_tmp =$_FILES['filename']['tmp_name'];
$file_type=$_FILES['filename']['type'];   
$file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));

if(isset($_POST['randomtext'])){
    $text_here = $_POST['randomtext'];
}

$expensions= array("jpeg","jpg","png");         
if(in_array($file_ext,$expensions)=== false){
    $errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
 $errors[]='File size must be excately 2 MB';
}          

// if there are no errors...     
if (empty($errors)==true) {

    // upload the file...
    move_uploaded_file($file_tmp,"uploads/".$file_name);

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "admin";

    // and create a new record in the database
    mysql_connect($servername, $username, $password) or die ('MySQL Not found // Could Not Connect.');
    mysql_select_db("admin") or die(mysql_error()) ;
    mysql_query("INSERT INTO upload_test (text, fileName) VALUES ('$text_here', '$file_name')") ;

    echo "Success";
    }else{
    print_r($errors);
    }
  }

 ?>

The form:

<form name="form" method="POST" enctype="multipart/form-data" >
 <input name="randomtext" type="text" id="randomtext" /><br/><br/>
 <input name="filename" type="file" id="filename" />
 <input name="submit" type="submit" id="submit"/>
</form>

There. the randomtext is a sample textfield.what if there are more of it?. Kindly help me, please. You can code your own but the goal is a form with 5 text fields and 1 filefield.the filename and other text are inserted to the db and the uploaded file is uploaded to a folder.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doufeng2877 doufeng2877 7年前

    You can add more field like :

    <input name="randomtext2" type="text" id="randomtext2" /><br/><br/>
    <input name="randomtext3" type="text" id="randomtext3" /><br/><br/>
    <input name="randomtext4" type="text" id="randomtext4" /><br/><br/>
    
    if(isset($_POST['randomtext2'])){
        $text2_here = $_POST['randomtext2'];
    }
    if(isset($_POST['randomtext3'])){
        $text3_here = $_POST['randomtext3'];
    }
    if(isset($_POST['randomtext4'])){
        $text4_here = $_POST['randomtext4'];
    }
    

    mysql_query("INSERT INTO upload_test (text, text2, text3, text4, fileName) VALUES ('$text_here', '$text2_here', '$text3_here', '$text4_here','$file_name')") ;

    You have to add in the database columns text2, text3, text4 too.

    点赞 评论 复制链接分享