dppx9253 2013-07-14 17:33
浏览 53
已采纳

无法在数据库中更新和/或插入图像(也有问题显示)

Hello I am here yet again with a new problem. So I have managed to get a code working where I upload pictures. However I both need to update an already existing row(I have added fields that can be used for pictures) and show the picture. For the moment however I can only update a small database dedicated to images. The problem with it is that it doesn't upload correctly. I get the error message "Could not upload for some reason!" but I don't know why.

The code how it currently looks:

<?php

session_start();

?>

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0" />
<title>TF2 NUT</title>
<link rel="stylesheet" href="css/style.css" />
</head>

<div id="Container">

<div id="header">
<h1>Welcome to TF2 NUT!</h1>
</div>

<div id="nav">

<?php

include_once "nav.php";

?>

</div>

<div id="Content">
<form action="CAPimg.php" method="POST" enctype="multipart/form-data">
Choose weapon to upload to: <br />
<input type="text" name="Name" id="Name" /><br />
<input type="file" name="image"><input type="submit" name="submit" value="Upload"     />
</form>
<?php

if(isset($_POST['submit'])) {

mysql_connect("localhost","root","") or die("Could not find database!");
mysql_select_db("weapons") or die("Could not find database!");

$Name = strip_tags ($_POST['Name']);
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);

if(substr($imageType,0,5) == "image") {
    mysql_query("UPDATE `weapon` VALUES Image_name='$imageName',     Image='$imageData' WHERE Name='$Name' AND Creator='$_SESSION[username]'") or die("Could not upload for some reason!");
    echo "Image uploaded!";
}
else
{
    echo "Only images are allowed!";
}

}

?>
<br />
<a href="subcentral.php">Return to submission central</a>
</div>

</div>

</html>

I get the "Creator" from a login that exist inside "nav.php". I have looked for guides but most of them either just covers file directories(I work with a remote server so I can not do it) and/or ASP. Yes, I know I should update my code to the new extensions but its currently to late for that. So if anyone can help me to both show my images(without refering to id like this:

<?php

mysql_connect("localhost","root","") or die("Could not find database!");
mysql_select_db("images") or die("Could not find database!");

if(isset($_GET['id'])) {

$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM `blob` WHERE `id`='$id'");

while($row = mysql_fetch_assoc($query)){

$imageData = $row["image"];


}
header("content_type: image/jpeg");
echo $imageData;

}
else
echo "Error!";

?>    

<html>
<body>

<img src="showimage.php?id=4">

</body>
</html>

My database contain multiple fields:

id, Creator, Name, Base_Damage, Damage, Pellets, Attackspeed, RS(first), RS(consecutive), Loaded, Reserve, Critical, Ability, Piece_set, Bonus_set, Description, Image_name, Image.

As you can see the last two fields are dedicated to images and thats what I want to update. When I write out the information contained in the row.

I would be happy if anyone help me with the first problem but if anyone can help me with both the problems I would be really happy(as I am new to PHP). Just like always if you want me to clarify anything or post any code I might have related to this just ask me.

  • 写回答

2条回答 默认 最新

  • drcvvkx914772 2013-07-14 17:48
    关注

    If you refer to the UPDATE documentation, you can see that the query syntax is wrong.

    The correct syntax is as follows:

    UPDATE table_name
    SET column1=value1,column2=value2,...
    WHERE some_column=some_value;
    

    You need to use SET instead of VALUES.

    Try:

    mysql_query("
    UPDATE `weapon` SET Image_name='$imageName',Image='$imageData' 
    WHERE Name='$Name' AND Creator='$_SESSION[username]';
    ") or die("Could not upload for some reason!");
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?