doulou0882 2014-02-05 14:33
浏览 31
已采纳

如何使用PHP同时提交文本和图像?

I'm currently trying to upload an image to a mysql database using php. The image should be inserted to the same row as the user. So, in my not-so-good attempt to do this, i have created a textbox and an upload form, so when the user enters his name on the textbox and uploaded an image they will be saved at the same time. The problem here is that I'm not quite sure if the method I've tried is right. well, I'm not really good in php to begin with. any help will do!

here is my form:

<?php

require('admin.config.inc.php');

if(isset($_POST['upload'])){
    $image_name = $_FILES['image']['name'];
    $image_type = $_FILES['image']['type'];
    $image_size = $_FILES['image']['size'];
    $image_tmp_name = $_FILES['image']['tmp_name'];

    $path = "/home/********/public_html/StagConnect/admin/pictures/$image_name";

    if($image_name==''){
    echo "Don't just click! select an image please .";
    exit();
    }
    else{
    move_uploaded_file($image_tmp_name, $path);
    $mysql_path = $path."/".$image_name;
    $query = "INSERT INTO `admin`(`admin_image1`,`path1`) VALUES ('$image_name','$mysql_path') where username = :user";

    $query_params = array(
    ':user' => $_POST['username'],
        ':image_name' => $image_name,
        ':mysql_path' => $path,
        );

    //execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't Upload Image!";
        die(json_encode($response));
    }

    $response["success"] = 1;
    $response["message"] = "Image Uploaded Succesfully!";
    echo json_encode($response);
   }
    }   
    ?>

<form action="adminProfilePic.php" method="post" enctype="multipart/form-data">
Username: <input type="text" name="username">
<input type="file" name="image" >

<input type="submit" name="upload" value="Submit" >
</form>

~also, I'm not quite sure if my query_params is correct.

  • 写回答

2条回答 默认 最新

  • duanfu7004 2014-02-05 17:24
    关注

    N.B.: There are 3 different types. 2 of which are different types of INSERT's and one UPDATE.

    I used user as the column name, so you may want to either keep it, or adjust it.

    I successfully tested this and do read throughout the code for additional comments.

    Assuming DB variable in admin.config.inc.php is set to $db - If not, then you will need to modify the variable to suit/match,

    As a regular INSERT use the following (An UPDATE version follows)

    <?php
    require('admin.config.inc.php');
    
    if(isset($_POST['upload'])){
        $image_name = $_FILES['image']['name'];
        $image_type = $_FILES['image']['type'];
        $image_size = $_FILES['image']['size'];
        $image_tmp_name = $_FILES['image']['tmp_name'];
    
        $path = "/home/********/public_html/StagConnect/admin/pictures/$image_name";
    
        if($image_name==''){
        echo "Don't just click! select an image please .";
        exit();
        }
        else{
        move_uploaded_file($image_tmp_name, $path);
        $mysql_path = $path."/".$image_name;
    
    // I am unsure of this line and how you are using it, so the UPDATE version follows.
    // Test with the line below that for now
    //  $stmt = $db->prepare("INSERT INTO `admin_test_so` (`admin_image1`,`path1`) VALUES (:image_name,:mysql_path) where username = :user");
    
        $stmt = $db->prepare("INSERT INTO `admin_test_so` (`admin_image1`,`path1`) VALUES (:image_name,:mysql_path)");
    
        try {
    
     $stmt->execute(array(
      ':image_name' => $image_name,
      ':mysql_path' => $path
    
        ));
    
    }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());
    
            //or just use this use this one:
            $response["success"] = 0;
            $response["message"] = "Database Error. Couldn't Upload Image!";
            die(json_encode($response));
        }
    
        $response["success"] = 1;
        $response["message"] = "Image Uploaded Succesfully!";
        echo json_encode($response);
    
    /* my own tests to show what is set or not */
    /*
    echo "<hr>";
    var_dump($image_name);
    echo "<br>";
    var_dump($path);
    echo "<br>";
    var_dump($_POST['username']);
    */
    
       }
        }   
    ?>
    

    As a UPDATE type, use the following:

    This will work with your WHERE clause, which will update a row if the user's name exists.

    Again, assuming DB variable is set to $db

    <?php
    require('admin.config.inc.php');
    
    $username=$_POST['username'];
    
    if(isset($_POST['upload'])){
        $image_name = $_FILES['image']['name'];
        $image_type = $_FILES['image']['type'];
        $image_size = $_FILES['image']['size'];
        $image_tmp_name = $_FILES['image']['tmp_name'];
    
        $path = "/home/********/public_html/StagConnect/admin/pictures/$image_name";
    
        if($image_name==''){
        echo "Don't just click! select an image please .";
        exit();
        }
        else{
        move_uploaded_file($image_tmp_name, $path);
        $mysql_path = $path."/".$image_name;
    
    $stmt = $db->prepare("UPDATE `admin_test_so` set admin_image1=:image_name, path1=:image_name where user = :user");
    
    // update test_table set value=:value, value0=:value0 where value=:value
    
        try {
    
     $stmt->execute(array(
      ':user' => $username,
      ':image_name' => $image_name,
      ':mysql_path' => $path
    
        ));
    
    }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());
    
            //or just use this use this one:
            $response["success"] = 0;
            $response["message"] = "Database Error. Couldn't Upload Image!";
            die(json_encode($response));
        }
    
        $response["success"] = 1;
        $response["message"] = "Image Uploaded Succesfully!";
        echo json_encode($response);
    
    /*
    echo "<hr>";
    var_dump($image_name);
    echo "<br>";
    var_dump($path);
    echo "<br>";
    var_dump($_POST['username']);
    */
    
       }
        }   
    ?>
    

    As an INSERT to also insert the username entered, use the following, which will enter 3 values.

    <?php
    require('admin.config.inc.php');
    
    $username=$_POST['username'];
    
    if(isset($_POST['upload'])){
        $image_name = $_FILES['image']['name'];
        $image_type = $_FILES['image']['type'];
        $image_size = $_FILES['image']['size'];
        $image_tmp_name = $_FILES['image']['tmp_name'];
    
        $path = "/home/********/public_html/StagConnect/admin/pictures/$image_name";
    
        if($image_name==''){
        echo "Don't just click! select an image please .";
        exit();
        }
        else{
        move_uploaded_file($image_tmp_name, $path);
        $mysql_path = $path."/".$image_name;
    
    $stmt = $db->prepare("INSERT INTO `admin_test_so` (`user`,`admin_image1`,`path1`) VALUES (:user,:image_name,:mysql_path)");
    
        try {
    
     $stmt->execute(array(
      ':user' => $username,
      ':image_name' => $image_name,
      ':mysql_path' => $path
    
        ));
    
    
    }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());
    
            //or just use this use this one:
            $response["success"] = 0;
            $response["message"] = "Database Error. Couldn't Upload Image!";
            die(json_encode($response));
        }
    
        $response["success"] = 1;
        $response["message"] = "Image Uploaded Succesfully!";
        echo json_encode($response);
    
    /*
    echo "<hr>";
    var_dump($image_name);
    echo "<br>";
    var_dump($path);
    echo "<br>";
    var_dump($_POST['username']);
    */
    
       }
        }   
    ?>
    

    HTML form

    <form action="adminProfilePic.php" method="post" enctype="multipart/form-data">
    Username: <input type="text" name="username">
    <input type="file" name="image" >
    <input type="submit" name="upload" value="Submit" >
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等