drlh197610 2014-11-29 11:16
浏览 96
已采纳

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 2014-11-29 11:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功