2

我对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>
4

3 回答 3

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 获取以前的图像值并更新。

于 2013-08-13T10:58:15.217 回答
1
<tr>
<td>image</td>
<td><input type="file" name="image" ></td>
</tr>
于 2013-08-13T10:54:57.100 回答
1

您必须使用input:type=fileelement 而不是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>
于 2013-08-13T11:00:13.130 回答