使用PDO将图像位置上传到数据库,将图像文件上传到目录? [英] Uploading Image Location to DB and Image File to Directory using PDO?

查看:47
本文介绍了使用PDO将图像位置上传到数据库,将图像文件上传到目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请为该示例代码提供一些帮助,尝试在将链接位置存储在数据库中的同时将图像添加到文件目录中,但使用PDO代替旧方法.

could I get some assistance with this example code please, trying to add an image to the file directory while storing the link location in the database, but using PDO instead of the old way.

它基于我在网上找到的示例,该示例带有dbconnect.php,save.php,addstudent.php以及此查询不需要的其他一些示例.

It is based on an example I found online which comes with a dbconnect.php, save.php, addstudent.php and some others not needed for this query.

    <form method="post" name="frmStudent" action="save.php">
    <input type="hidden" name="pid" value="<?php echo $ppid; ?>"/>
        <table>
            <tr><td>First Name</td><td>:</td><td><input type="text" name="fname" required="required" value="<?php echo $pfname; ?>"/></td></tr>
            <tr><td>Last Name</td><td>:</td><td><input type="text" name="lname" required="required" value="<?php echo $plname; ?>"/></td></tr>
            <tr><td>Contact No.</td><td>:</td><td><input type="tel" name="contact" required="required" value="<?php echo $pcontact; ?>"/></td></tr>
            <tr><td>Email</td><td>:</td><td><input type="email" name="email" required="required" value="<?php echo $pemail; ?>"/></td></tr>
             <tr><td>Image</td><td>:</td><td><input type="file" name="email" required="required" value="<?php echo $pimg_url; ?>"/></td></tr>
            <tr><td></td><td></td><td><input type="submit" class="myButton" value="Save"/></td></tr>
        </table>
    </form>

这是保存到数据库中的代码

This is the code that saves to the database

<?php   
error_reporting(0);
    include ("dbconnection.php");
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $contact=$_POST['contact'];
    $email=$_POST['email'];
    $img_url=$_POST['img_url'];
    $id=$_POST['pid'];
    if($id==null){
            $sql="INSERT INTO student(fname,lname,contact,email,img_url)values(:fname,:lname,:contact,:email,:img_url)";
            $qry=$db->prepare($sql);
            $qry->execute(array(':fname'=>$fname,':lname'=>$lname,':contact'=>$contact,':email'=>$email,':img_url'=>$img_url));
    }else{
            $sql="UPDATE student SET fname=?, lname=?, contact=?, email=?, img_url=? where id=?";
            $qry=$db->prepare($sql);
            $qry->execute(array($fname,$lname,$contact,$email,$img_url,$id));   
    }
    echo "<script language='javascript' type='text/javascript'>alert('Successfully Saved!')</script>";
    echo "<script language='javascript' type='text/javascript'>window.open('index.php','_self')</script>";
?>

非常感谢您对如何执行此操作有任何见解,谢谢.

Any insights on how to do this would be much appreciated, Thanks.

推荐答案

首先从修复图像的HTML输入开始

First of all start by fixing the HTML input for the image

<tr>
    <td>Image</td><td>:</td>
    <td><input type="file" name="image" required="required" value=""/></td>
</tr>

然后在您的PHP代码上展开:

then expand on your PHP code:

<?php   
error_reporting(0);
include ("dbconnection.php");

if(is_uploaded_file($_FILES['image']['tmp_name'])){ 
    $folder = "upload/"; 
    $file = basename( $_FILES['image']['name']); 
    $full_path = $folder.$file; 
    if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) { 
        echo "succesful upload, we have an image!";
        $fname=$_POST['fname'];
        $lname=$_POST['lname'];
        $contact=$_POST['contact'];
        $email=$_POST['email'];
        $img_url= $full_path;
        $id=$_POST['pid'];
        if($id==null){
            $sql="INSERT INTO student(fname,lname,contact,email,img_url)values(:fname,:lname,:contact,:email,:img_url)";
            $qry=$db->prepare($sql);
            $success = $qry->execute(array(':fname'=>$fname,':lname'=>$lname,':contact'=>$contact,':email'=>$email,':img_url'=>$full_path));
        }else{
            $sql="UPDATE student SET fname=?, lname=?, contact=?, email=?, img_url=? where id=?";
            $qry=$db->prepare($sql);
            $success = $qry->execute(array($fname,$lname,$contact,$email,$full_path,$id));   
        }

        if($success){
            echo "<script language='javascript' type='text/javascript'>alert('Successfully Saved!')</script>";
            echo "<script language='javascript' type='text/javascript'>window.open('index.php','_self')</script>";
        }else{
            echo 'db transaction failed';
        }
    } else { 
       echo "upload received! but process failed";
    } 
}else{ 
    echo "upload failure ! Nothing was uploaded";
} 
?>

这篇关于使用PDO将图像位置上传到数据库,将图像文件上传到目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆