使用输入文件类型编辑图像

时间:2013-08-13 10:51:12

标签: php mysql image file edit

我做了一个简单的编辑,以便在mysql中编辑数据,一切正常,除非我想编辑输入文件类型图像它不起作用,它不会给出错误消息它只是不编辑任何东西当我删除输入文件类型图像时,它的工作原理。 并且通过编辑图像我的意思是输入一个新图像将替换旧图像。

这是我的代码:

<?php

require("db.php");

$id     = $_REQUEST['theId'];
$result = mysql_query("SELECT * FROM table WHERE id  = '$id'");
$test   = mysql_fetch_array($result);

$name   = $test['Name'] ;
$email  = $test['Email'] ;                  
$image  = $test['Image'] ;

if (isset($_POST['submit']))
{   
    $name_save  = $_POST['name'];
    $email_save = $_POST['email'];

    if (isset($_FILES['image']['tmp_name']))
    {
        $file       = $_FILES['image']['tmp_name'];
        $image      = addslashes(file_get_contents($_FILES['image']['tmp_name']));
        $image_name = addslashes($_FILES['image']['name']);

        move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);
        $image_save ="photos/" . $_FILES["image"]["name"];

        mysql_query("UPDATE table SET Name ='$name_save', Email  ='$email_save',Image ='$image_save' WHERE id = '$id'") or die(mysql_error()); 

        header("Location: index.php");
    }
}
?>



<form method="post">
    <table>
        <tr>
            <td>name:</td>
            <td>
                <input type="text" name="name" value="<?php echo $name ?>"/>
            </td>
        </tr>
        <tr>
            <td>email</td>
            <td>
                <input type="text" name="email" value="<?php echo $email ?>"/>
            </td>
        </tr>
        <tr>
            <td>image</td>
            <td>
                <input type="file" name="image" value="<?php echo $image ?>"/>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" name="submit" value="submit" />
            </td>
        </tr>
    </table>

3 个答案:

答案 0 :(得分:3)

在表单中,enctype =“multipart / form-data”丢失,并且在您的表单中没有type =“file”。

提供以下代码并尝试。

<?php
require("db.php");
$id =$_REQUEST['theId'];

$result = mysql_query("SELECT * FROM table WHERE id  = '$id'");
$test = mysql_fetch_array($result);

$name=$test['Name'] ;
$email= $test['Email'] ;                    
$image=$test['Image'] ;

if(isset($_POST['submit'])){    
$name_save = $_POST['name'];
$email_save = $_POST['email'];
$image_save=$image //Added if image is not chose from the form post

if (isset($_FILES['image']['tmp_name'])) {
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);
$image_save ="photos/" . $_FILES["image"]["name"];
}
mysql_query("UPDATE table SET Name ='$name_save', Email  ='$email_save',Image ='$image_save' WHERE id = '$id'")
or die(mysql_error()); 
header("Location: index.php");      }
?>



<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="name" value="<?php echo $name ?>"/></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email" value="<?php echo $email ?>"/></td>
</tr>

<tr>
<td>image</td>
<td><input type="file" name="image" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>

此外,如果在更新时未选择图像,则应通过sql获取之前的图像值并进行更新。

答案 1 :(得分:1)

<tr>
<td>image</td>
<td><input type="file" name="image" ></td>
</tr>

答案 2 :(得分:1)

您必须使用input:type=file元素而不是input:type=text才能使用$_FILES处理图像文件。或者你无法获得图像文件。所以你的if语句返回false并且没有任何反应。

<form method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>name:</td>
            <td><input type="text" name="name" value="<?php echo $name ?>"/></td>
        </tr>
        <tr>
            <td>email</td>
            <td><input type="text" name="email" value="<?php echo $email ?>"/></td>
        </tr>

        <tr>
            <td>image</td>
            <td><input type="file" name="image" /></td>
        </tr>
        <tr>
            <td>image preview</td>
            <td><img src="photos/<?php echo $image ?>" /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" value="submit" /></td>
        </tr>
    </table>
</form>