troubleshooting Question

Inserting images in database PDO

Avatar of sanchit gupta
sanchit gupta asked on
PHP
16 Comments2 Solutions1303 ViewsLast Modified:
I have created a very basic form for inserting data in database PDO. I'm done with the url, name and title but got stucked with the image. How to get image inserted into database through the "folder". I also want to fetch all the details onto some another page.
Any help in styling up these would also be highly appreciated.
<?php
ob_start();
include ("./inc/header.inc.php");
$fileToUpload="";
if(isset($_POST['newpost'])) {
    $urlInput=$_POST['urlInput'];
    $fileToUpload=$_FILES['fileToUpload']['name'];
    $title=$_POST['title'];
    $userid = Login::isLoggedin();
    $filename = $_FILES['fileToUpload']['name'];
    $filetemp = $_FILES['fileToUpload']['tmp_name'];
    $filesize = $_FILES['fileToUpload']['size'];
    $filebasename = basename($_FILES['fileToUpload']['name']);
    $dir="userdata/folder/";
    $finaldir=$dir.$filebasename;
    move_uploaded_file($filetemp,$finaldir);
    DB::query('INSERT INTO links VALUES (\'\', :urlInput, :fileToUpload, :title, NOW(), :userid, 0)', array(':urlInput'=>$urlInput, ':fileToUpload'=>$fileToUpload, ':title'=>$title, ':userid'=>$userid));
}

?>

<form action="submit.php" method="post">
        <p><label><b>URL</b></label><br>
<input type="url" name="urlInput" value="" placeholder="" required><p />
        <p><label><b>Image</b></label><br>
<input type="file" name="fileToUpload" value="" placeholder="" accept="image/*" required><p />
        <p><label><b>Title</b></label><br>
<input type="text" name="title" value="" placeholder="" required><p />
<input type = "submit" name="newpost" value="Submit"/>
</form>
ASKER CERTIFIED SOLUTION
Imran Ali
Web Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros