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

时间:2014-02-05 14:33:25

标签: php mysql

我目前正在尝试使用php将图像上传到mysql数据库。应将图像插入与用户相同的行。因此,在我不太好的尝试中,我创建了一个文本框和上传表单,因此当用户在文本框中输入他的名字并上传图像时,他们将同时保存。这里的问题是我不太确定我尝试的方法是否正确。好吧,我开始时并不擅长php。任何帮助都会做!

这是我的表格:

<?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>

〜也,我不太确定我的query_params是否正确。

2 个答案:

答案 0 :(得分:0)

您尝试绑定:image_name:mysql_path,但是将其直接插入查询中。将$image_name更改为:image_name,将$mysql_path更改为:mysql_path

$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,
    );

答案 1 :(得分:0)

N.B。:有3种不同的类型。其中2个是不同类型的INSERT和一个UPDATE

我使用user作为列名,因此您可能希望保留它或调整它。

我成功测试了此,并在整个代码中阅读了其他评论。

假设admin.config.inc.php中的DB变量设置为$db - 如果不是,那么您需要修改变量以适合/匹配,

作为常规INSERT使用以下内容(随后是UPDATE版本)

<?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']);
*/

   }
    }   
?>

作为UPDATE类型,请使用以下内容:

这将适用于您的WHERE子句,如果用户名存在,则会更新一行。

同样,假设DB变量设置为$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']);
*/

   }
    }   
?>

作为INSERT同时插入输入的用户名,请使用以下内容输入3个值。

<?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 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>