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

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

问题描述:

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.

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!

You are getting the error message "Could not upload for some reason!" Because, you have told your mysql query to show you that, incase the query fails, instead of the asking it for the real error, by doing or die(mysql_error()) here

  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!");

So, you need to change the above code to adding or die(mysql_error())

 mysql_query("UPDATE weapon SET Image_name='$imagename', Image='$imageData' WHERE Name='$Name' AND Creator='$_SESSION[username]' ")
                  or die(mysql_error());

another thing, the UPDATE statement does not need VALUE, it should simply be UPDATE weapon SET Image_name='$imagename', Image='$imageData' WHERE Name='$Name'

Even if you do all the above, as I have mentioned, your database, still be weak to mysql injections, therefore, you are better of learning PDO or mysqli