Simple upload of image to database for user registration

using php and pdo i managed to create registration page but without saving image

$firstname = trim($_POST['fn']); //at a minimus clear whitespace.
        $lastname = trim($_POST['ln']);
        $username = trim($_POST['un']);
        $password =  trim($_POST['pw']);
        $confirmpassword=  trim($_POST['cp']);
        $stmt = $dbh->prepare("INSERT INTO registration (fname,lname,username,password) VALUES (?,?,?,?)");
        $stmt->bindValue(1,$firstname,PDO::PARAM_STR);
        $stmt->bindValue(2,$lastname,PDO::PARAM_STR);
        $stmt->bindValue(3,$username,PDO::PARAM_STR);
        $stmt->bindValue(4,$password,PDO::PARAM_STR);
        if($stmt->execute()){
        echo "YOUR REGISTRATION IS COMPLETED...";
        }

      

I found this tutorial , but it is too hard to understand and has not been clearly explained. I'm looking for ideas or tutorials that are easy to figure out how to upload an image. Any idea is consumed.

The form

<form method="POST" action="crud.php">
                <tr>
                        <td>

                        </td>
                        <td>
                            <input type="file" name="image" />
                        </td>
                </tr>
                <tr>
                        <td>First Name</td>
                        <td>
                            <input type="text" name="fn">
                        </td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td>
                        <input type="text" name="ln">
                    </td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td>
                        <input type="text" name="un">
                    </td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td>
                        <input type="password" name="pw">
                    </td>
                </tr>
                <tr>
                    <td>Confirm Password</td>
                    <td>
                        <input type="password" name="cp">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input id="button" type="submit" value="Back" name="back"/>
                    </td>
                    <td>
                        <input id="button" type="submit" value="SignUp" name="signup"/>
                    </td>
                </tr>
                <tr><td><div style="font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div></td></tr>
                </form>

      

+3


source to share


2 answers


Here's a simple example of how to achieve this:

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!";

        $firstname = trim($_POST['fn']); 
        $lastname = trim($_POST['ln']);
        $username = trim($_POST['un']);
        $password =  trim($_POST['pw']);
        $confirmpassword=  trim($_POST['cp']);

        $stmt = $dbh->prepare("INSERT INTO registration (fname,lname,username,password, img_url) VALUES (?,?,?,?,?)");
        $stmt->bindValue(1,$firstname,PDO::PARAM_STR);
        $stmt->bindValue(2,$lastname,PDO::PARAM_STR);
        $stmt->bindValue(3,$username,PDO::PARAM_STR);
        $stmt->bindValue(4,$password,PDO::PARAM_STR);
        $stmt->bindValue(5,$full_path,PDO::PARAM_STR);

        if($stmt->execute()){
            echo "YOUR REGISTRATION IS COMPLETED...";
        }else{
            echo 'YOUR REGISTRATION COULD NOT BE COMPLETED...';
        }

    } else { 
       echo "upload received! but process failed";
    } 
}else{ 
    echo "upload failure ! Nothing was uploaded";
} 

      



In the request, I have included a field titled img_url

.

The PDO request is executed after the image has been loaded successfully.

+3


source


You can just use the simple PHP upload () function. Here is an example http://www.w3schools.com/php/php_file_upload.asp



+1


source







All Articles