editing image by using input file type

China☆狼群 提交于 2019-12-06 09:19:23

In form enctype="multipart/form-data" is missing and in your form there is no type="file".

Give the below code and try.

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

Moreover you should get the previous image value through sql and update if image is not chose while updating.

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

You have to use input:type=file element instead of input:type=text in order to handle image file using $_FILES. Or you cannot get the image file. So your if statement returns false and nothing happens.

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!