使用 php 和 mysql 将图像更新到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12960727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating image into databse using php and mysql
提问by trouble creator
i want to store an image in database not by inserting but by updating (by using UPDATE query).
我想不是通过插入而是通过更新(通过使用 UPDATE 查询)将图像存储在数据库中。
Everything else is updated successfully but its not storing image. I'm using medium blob data type to store image.
其他所有内容均已成功更新,但未存储图像。我正在使用中等 blob 数据类型来存储图像。
Here is my code:
这是我的代码:
update-profile.php:
更新-profile.php:
<form id="form" method="post" action="update-profile-action.php" enctype="multipart/form-data">
<label for="Fname">First Name:</label> <input type="text" id="Fname" class="text" value="<?php echo $firstname; ?>" name="Fname" /> <br /><br />
<label for="Lname">Last Name:</label> <input type="text" id="Lname" class="text" value="<?php echo $lastname; ?>" name="Lname" /><br /> <br />
<?php
if ($_SESSION["type"]=="T")
{
?>
<label>Profile Image:</label> <input type="file" name="image" value="" /><br /> <br />
<label>Qualification:</label><textarea name="qualification" class="text" id="qualification"><?php echo $qualification;?></textarea><br /><br />
<label>Education & Teaching History:</label> <textarea name="briefintro" class="text" id="intro"><?php echo $briefintro; ?></textarea><br /><br />
<?php
}
?>
<input type="submit" class="mybutton" value="Update Profile" />
</form>
update-profile-action.php:
更新配置文件-action.php:
<?php include("../includes/config.php");?>
<?php
$Fname=$_POST["Fname"];
$Lname=$_POST["Lname"];
$image=$_POST["profileimg"];
$briefintro=$_POST["briefintro"];
$qualification=$_POST["qualification"];
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$query=("UPDATE accounts SET firstname='".$Fname."' , lastname='".$Lname."' , profileimg='".$image."' , briefintro='".$briefintro."', qualification='".$qualification."' WHERE id=".$_SESSION['id']);
$result = mysql_query($query);
header("Location: update-profile.php?status=3");
mysql_close($con);
?>
i copied only the related data from update-profile.php to making it more easy to read :) any kind of help will be appreciated :)
我只复制了 update-profile.php 中的相关数据以使其更易于阅读:) 任何形式的帮助将不胜感激:)
采纳答案by BOMEz
Two problems:
两个问题:
You do not have an id for the input tag for the file. You need to change <input type="file" name="image" value="" />to <input type="file" name="image" value="" id = "profileimage" />
您没有该文件的输入标签的 ID。您需要更改<input type="file" name="image" value="" />为<input type="file" name="image" value="" id = "profileimage" />
Secondly you access files not via $_POSTbut rather via $_FILES.
其次,您不是通过$_POST而是通过$_FILES.

