LecheDeCrema LecheDeCrema -4 years ago 105
PHP Question

How can I save an Image and a text separately in php mysqli

Hi i'm having a hard time in saving a text with an input tag and an image using one button.

here is my form:

<form method = "POST" action = "image_upload_featured.php" enctype = "multipart/form-data">
<label>Drag or click for image</label>
<div id="uploader" onclick="$('#photo').click()">
<img src=""/>
</div>
<input type="file" name="image" id="photo"/>
<div id = "file_name"></div>
<button class = "btn btn-primary" name = "save"><span class = "glyphicon glyphicon-download"></span> Save Image</button>


<input class = "w3-input w3-border" type = "text" name= "fullname" placeholder = "Fullname" style="margin-bottom: 15px;" required>
<input class = "w3-input w3-border" type = "text" name= "lname" placeholder = "Lastname" style="margin-bottom: 15px;" required>
</form>


I have a php file where it can saved the image

<?php
include('db/database_configuration.php');
if(ISSET($_POST['save'])){

if($_FILES['image']['name'] == ""){
echo '<script>alert("Please Select an Image")</script>';
echo '<script>window.location = "add_featured_alumni.php"</script>';
}else{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
move_uploaded_file($_FILES["image"]["tmp_name"], "featured_image/". $_FILES["image"]["name"]);
$location = $_FILES["image"]["name"];

$stmt = $conn->prepare("INSERT INTO `tblfeatured` (image1) VALUES(?)") or die(mysqli_error());
$stmt->bind_param("s", $location);

if($stmt->execute()){
$stmt->close();
$conn->close();
echo '<script>alert("Successfully Upload Image")</script>';
echo '<script>window.location = "add_featured_alumni.php"</script>';
}else{
echo '<script>alert("Error")</script>';
}
}
}
?>


But this is only for the image, I know also how to save a text using input tag but without image.

I would like to combine both. Can anyone please help me?

Answer Source

I assume that you have fullname and lname fields (columns) are in your Database table along with image1. Your Html

<form method = "POST" action = "image_upload_featured.php" enctype = "multipart/form-data"> 
    <label>Drag or click for image</label>
       <div id="uploader" onclick="$('#photo').click()">
    <img src=""/>
    </div>
    <input type="file" name="image"  id="photo"/>
    <div id = "file_name"></div>

    <input class = "w3-input w3-border" type = "text" name= "fullname" placeholder = "Fullname" style="margin-bottom: 15px;" required >
    <input class = "w3-input w3-border" type = "text" name= "lname" placeholder = "Lastname" style="margin-bottom: 15px;" required >
    <button type="submit" class = "btn btn-primary" name = "save"><span class = "glyphicon glyphicon-download"></span> Save Image</button>
</form>

PHP

<?php
include('db/database_configuration.php');
if(ISSET($_POST['save'])){

    if($_FILES['image']['name'] == ""){
            echo '<script>alert("Please Select an Image")</script>';
            echo '<script>window.location = "add_featured_alumni.php"</script>';
    }else{  
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
        $image_name = addslashes($_FILES['image']['name']);
        $image_size = getimagesize($_FILES['image']['tmp_name']);
        move_uploaded_file($_FILES["image"]["tmp_name"], "featured_image/". $_FILES["image"]["name"]);
        $location = $_FILES["image"]["name"];
        //edit.....get input values
        $fullname = $_POST['fullname'];
        $lname = $_POST['lname'];
        $stmt = $conn->prepare("INSERT INTO `tblfeatured` (image1,fullname,lname) VALUES(?,?,?)") or die(mysqli_error($conn));
        $stmt->bind_param("sss", $location,$fullname,$lname); //bind to param
        //........................................

        if($stmt->execute()){
            $stmt->close();
            $conn->close();
            echo '<script>alert("Successfully Upload Image")</script>';
            echo '<script>window.location = "add_featured_alumni.php"</script>';
        }else{
            echo '<script>alert("Error")</script>';
        }
    }   
}
?>

NOTE: This must work except exceptional syntax error. (If your code was previously working then this must also work)
Your DB Table must be something like this
| id | image1 | fullname | lname |

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download