如何在php mysqli中单独保存图像和文本

时间:2017-02-01 15:12:56

标签: php mysqli

您好我很难用一个按钮保存带有输入标签和图像的文字。

这是我的表格:

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

我有一个可以保存图像的php文件

.csv

但这仅适用于图像,我也知道如何使用输入标签保存文本但没有图像。

我想两者结合起来。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

我假设您的数据库表中包含fullnamelname字段(列)以及image1。 你的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>';
        }
    }   
}
?>

注意:除非出现异常语法错误,否则必须正常工作。 (如果您的代码以前正在运行,那么这也必须有效)
你的数据库表必须是这样的 的 | id | image1 |全名| lname |