doutao5499 2018-06-12 12:42
浏览 206

如何在php中保存文件和一些数据

I have html input fields.

Those input fields are

1.name

2.title

3.lyrics

4.upload

Now I need to save name, title, lyrics and upload files into database at same time.How to do this??

Here I share my codes

 <?php
 $connection = mysqli_connect("localhost", "root", "upload"); // 
     Establishing Connection with Server
   $db = mysqli_select_db($connection, "song"); // Selecting Database from 
 Server
  if(isset($_POST['submit'])){ // Fetching variables of the form which 
  travels in URL
  $name = $_POST['name'];
  $title = $_POST['title'];
  $lyrics = $_POST['lyrics'];


   if($name !=''||$title !=''||$lyrics !=''){
   //Insert Query of SQL
   $query = mysqli_query($connection, "insert into users(name, title, 
   lyrics) values ('$name', '$title', '$lyrics')");
   echo "<br/><br/><span>Data Inserted successfully...!!</span>";
   }
   else{
   echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
   }
   }
   mysqli_close($connection); // Closing Connection with Server
   ?>



   <div style="margin:1em auto; width:333px; text-align:center;">
    <form method="post" enctype="multipart/form-data">

   <table style="margin-top: 200px;line-height: 27px;">
     <tr><td><label for='name' >Name: </label></td>
         <td> <input type="text" name="name"></td></tr>

        <tr><td>   <label for='title' >Title: </label></td>
       <td> <input type="text" name="title"></td></tr>

         <tr><td>  <label for='lyrics' >lyrics: </label></td>
        <td><textarea cols="25" name="lyrics" rows="5"></textarea></td></tr>

         <tr> <td>  <label for='file'>upload:</label></td>
         <td><input type="file" name="files" multiple="multiple"></td>
        <td><input type="submit" name="upload" value="upload!" ></td></tr>
       </table>
              <button class="submit" name="submit" type="submit" 
       value="Insert">save</button>
        </form>
        </div>

I will save name, title, lyrics data into database.

But when I try to save upload file into database, it shows an error

  • 写回答

3条回答 默认 最新

  • dongtai419309 2018-06-12 12:51
    关注

    Use this

    $info = pathinfo($_FILES['userFile']['name']);
    $ext = $info['extension']; // get the extension of the file
    $newname = "newname.".$ext; 
    
    $target = 'images/'.$newname;
    move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探