Biax20
Biax20

Reputation: 89

How to upload a Photo or a file in MYSQL in a specific row?

I have a form which I let my user to enter his email and upload a photo. Let say that my user already have an existing email in my database, and he want to upload a photo of him. Now the question how to do it? How to get the value of my input type = "email" and use it to my query like this :

$query = "insert into user (Photo) values ('".$file."') where email = "$_GET['email']"";

I try this query but it doesn't work what should I do?

EDIT: this is my entire code

<form class = "container" method = "POST" action = "upload.php" enctype = "multipart/form-data">
                <input name = "mang" type = "email" class = "form-control w-75" placeholder="Put Your EMAIL here">
                <center> <input class = "form-control w-75" type = "file" name = "photo">
                <button class = "btn" type = "submit" name = "Upload">Upload</button>
                </form>
<?php
include'connect.php';
// File upload path

if(isset($_POST["Upload"]) && !empty($_FILES["photo"]["name"])){
    $targetDir = "image/";
    $fileName = $_FILES["photo"]["name"];
    $targetFilePath = $targetDir . $fileName;
    $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','pdf');
    if(in_array($fileType, $allowTypes)){
        // Upload file to server
        if(move_uploaded_file($_FILES["photo"]["tmp_name"], $targetFilePath)){
            // Insert image file name into database
            $insert =$connect->query("insert into user (Photo) values ('".$file."') where email = "$_GET['email']"");
            
            if($insert){
                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
            }
            else{
                $statusMsg = "File upload failed, please try again.";
            } 
        else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    else{
        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
    }
else{
    $statusMsg = 'Please select a file to upload.'; 
}

// Display status message
print "<p>" .$statusMsg. "</p>";
?>
<div>

</div>

Upvotes: 1

Views: 537

Answers (1)

Santosh Dangare
Santosh Dangare

Reputation: 755

There is a mistake in your form, you have used method POST and using GET method to get the value. so use $_POST variable to get the value echo $_POST['email'];

In your case, your email field has name as mang so use $_POST['mang']; to get email field value.

Upvotes: 1

Related Questions