when storing image in mysql database i have error
警告:mysql_connect():在第37行的C: xampp htdocs ramya store.php中对用户'root'@'localhost'(使用密码:YES)拒绝访问“无法连接localhost:拒绝用户root的访问'@'localhost'(使用密码:是)
<?php
error_reporting(E_ALL);
$server = "localhost";
$login = "root";
$s_password = " ";
$hotel_name=$_POST['hotel_name'];
$street_name=$_POST['street_name'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$zipcode=$_POST['zipcode'];
$phone_number=$_POST['phone_number'];
$fax=$_POST['fax'];
$email_id=$_POST['email_id'];
$pass=$_POST['password'];
foreach ($_FILES["pictures"]["error"] as $key => $error) { //used for multiple uploads
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
}
}
$size = getimagesize($tmp_name);
$width = $size[0]; // get width of the image
$height = $size[1]; //get height of the image
$type = $size[2]; //get type of the image
$mime_type = $size['mime']; //get MIME of the image
if(!$data = addslashes(@fread(@fopen($tmp_name, "r"), @filesize($tmp_name)))){
die("n<BR>Cannot read temp file: $tmp_file");
}
$link = mysql_connect($server, $login, $s_password);
if (!$link) {
die("n<BR>Could not connect $server:" . mysql_error());
}
$db_selected = mysql_select_db("test");
if (!$db_selected) {
die ("n<BR>Can't use Table : $db_selected" . mysql_error());
}
$query = "INSERT INTO image_data ";
$query .= " (hotel_name,street_name,city,state,country,zipcode,phone_number,fax,email_id,password,image_type, image_width, image_height, image_data) ";
$query .= " values ";
$query .= " ('$hotel_name','$street_name','$city','$state','$country','$zipcode','$phone_number','$fax','$email_id','$pass','$mime_type', '$width', '$height', '$data') ";
$result = mysql_query($query);
if (!$result) {
$message = '<BR>Invalid query: ' . mysql_error() . "n";
die($message);
}
$image_id = mysql_insert_id() ;
echo "n<IMG SRC="getimage.php?id=$image_id" />";
mysql_close($link);
exit();
?>
请检查密码中是否有空格,请尝试删除它
$s_password = "";
This specific error has nothing to do with your code. You're not getting logged into MySQL in the first place. Make sure your account has access.
Also, this entire script is extremely insecure. Use prepared/parameterized queries to avoid SQL injection attacks. The way you are handling the data now probably won't even work at all. addslashes()
isn't sufficient.
The error means exactly what it says. It's not a problem with your PHP file - it's that you don't actually have permission to connect to the MySQL server that you're trying to.
root
user? That being said, there are many problems with your PHP file as well. The most obvious is simply that you're using the mysql_*
functions. These should not be used in new code becasue they are no longer maintained and are officially deprecated. (See the red box ?)
You should be using PDO or MySQLi instead.
The second problem is that you ar extremely vulnerable to SQL injection. This could be fixed by calling mysql_escape_string()
on each variable you include in your query, but a better solution is to switch to PDO or MySQLi and investigate prepared statements.