dsrbb20862 2015-03-27 05:33
浏览 35

使用php将图像上传到mysql和特定目录

This is my code for upload.php, i want to upload the image in a directory and save its path to mysql database.

The image is successfully uploaded to the directory, but its entry doesn't get inserted into the database.

  • upload.php
<?php

require("connect.php");

if (isset($_FILES["userfile"]) && !empty($_FILES["userfile"])) {                                        
$image = $_FILES['userfile']['tmp_name'];                                   
$imageName = $_FILES['userfile']['name'];                                   
$about = $_POST['about'];                                   
$title = $_POST['title'];                                       
$place = $_POST['place'];                                       
$date = $_POST['date'];                                     
$time = $_POST['time'];                                     
$link = $_POST['link'];                                 
$details = $_POST['details'];                                       
$con = $_POST['con'];                                   
$email = $_POST['email'];                                       
$number = $_POST['number'];                                 
$len = count($image);                                   
$path = "admin/news/";                                  
for ($i = 0; $i < $len; $i++) {                                      
    if (isset($imageName[$i]) && $imageName[$i] !== NULL) {
        if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
             mysqli_query($con,"insert into tblnews (newsid, about,    date_of_event, time, link, event_place, title, details, image, date, contactperson, email, number) values('','$about','$date','$time','$link','$place','$title','$details','$imageName[1]',NOW(),'$con','$email','$number')");
                   echo"<script>alert('The news had been successfuly uploaded.')
                   window.location='index.php?pg=homepage'</script>";
                                 }
                             }
               }
}
?>
  • HTML form
<form enctype="multipart/form-data" method="post" action="upload.php">
    <table>
        <tr>
            <th>About</th>
            <td>
                <select name="about">
                    <option disabled  selected>Select</option>
                    <option value="Taal">Taal Volcano</option>
                    <option value="Malarayat">Mt. Malarayat Forest Reserve</option>
                    <option value="vip">Verde Island Passage</option>
                </select>
            </td>
        </tr>
        <tr>
            <th>Title</th>
            <td><input type="text" name="title" required ></input></td>
        </tr>
        <tr>
            <th>Event's Place</th>
            <td><input type="text" name="place" required  ></input></td>
        </tr>
        <tr>
            <th>Event Date</th>
            <td><input type="date" name="date" required></input></td>
        </tr>
        <tr>
            <th>Event Time</th>
            <td><input type="time" name="time"></input></td>
        </tr>
        <tr>
            <th>Image</th>
            <td><input name="userfile[]" type="file" multiple='multiple' />
            </td>
        </tr>
        <tr>
            <th>Link on Facebook</th>
            <td><input type="text" name="link"></input></td>
            <td><span>Go to the facebook page of the event and copy the url and paste it in the textbox.</span></td>
        </tr>
        <tr>
            <th>Details</th>
            <td><textarea name="details" style="height:250px;" ></textarea></td>
        </tr>
        <tr>
            <th>Contact Person</th>
            <td><input type="text" name="con" required  ></input></td>
        </tr>
        <tr> 
            <th>Email</th>
            <td><input type="email" name="email" required  ></input></td>
        </tr>
        <tr>
            <th>Number</th>
            <td><input type="text" name="number" ></input></td>
        </tr>
        <tr>
            <th></th>
            <td><input type="submit" name="add_news" value="Upload"></input></td>
        </tr>
    </table>
</form>
  • 写回答

1条回答 默认 最新

  • doupin5408 2015-03-27 06:17
    关注

    Try using prepared statement instead of inserting variable directly into your query.

    And as @ajaykumartak pointed out newsid is likely an auto-increment key and do not need to be set in insertion.

    Modification to your code:

    if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
        $stmt = mysqli_stmt_init($con);
        $query = <<<END 
            insert into tblnews (
                about,
                date_of_event, 
                time, 
                link,
                event_place, 
                title, 
                details, 
                image, 
                date, 
                contactperson, 
                email, 
                number
            )
            values(?,?,?,?,?,?,?,?,?,?,?,?);
    END;
        mysqli_stmt_prepare($stmt,$query);
        mysqli_stmt_bind_param($stmt,'ssssssssssss',
             $about, $date, $time, $link,
             $place, $tile, $details, $imageName[$i],
             $date, $contactperson, $email, $number
        );
        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);
    }
    

    Of course you should also check for the return values of the function call to verify that they execute correctly, because I only inferred your table structure. You would need to check the error with mysqli_error :

    echo ( mysqli_error($con) );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么