Loading image to database and image file to directory using PDO?

Can I get some help with this code example trying to add an image to a file directory while keeping the link location in the database, but using PDO instead of the old one.

It is based on an example I found on the internet that comes with dbconnect.php, save.php, addstudent.php and some others that are not needed for this request.

    <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 is saved in 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 ideas on how to do this would be much appreciated, thanks.

+3


source to share


2 answers


First of all, start by locking the input HTML

for the image

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

      



then expand the code PHP

:

<?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";
} 
?>

      

+2


source


For people who can't get Meda to respond to work, just add enctype="multipart/form-data"

to element <form>

.



+5


source







All Articles