修改将数据插入表中,使其现在为更新表

时间:2014-04-10 22:27:23

标签: php mysql sql-update

现在我将blob文件插入数据库。我已经阅读了关于mysql的更新语法我无法弄清楚如何修改我的代码以使用BLOB更新行而不是使用BLOB插入新行。有人可以帮我这个吗?

这是我的代码:

<?php
// Create MySQL login values and
// set them to your login information.
$username = "root";
$password = "";
$host = "localhost";
$database = "test";
$tbl_name="members";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);


// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


// Create the query and insert
// into our database.
$query = "INSERT INTO members ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);

// Print results
print "Thank you, your file has been uploaded.";

}
else {
print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);



?>

1 个答案:

答案 0 :(得分:0)

1°您需要为您要更新的数据传递参考,例如表中的主键ID。

2°更新SQL应该像它一样

$image = mysql_real_escape_string($unsafe_image);
$id    = mysql_real_escape_string($unsafe_id);
$query = "UPDATE members SET image = '$data' WHERE id_image = $id";
$results = mysql_query($query, $link);