当在mysql数据库中存储图像时,我有错误
警告: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 = "";
这个特定的错误与你的代码无关。 你并没有首先登录到MySQL。 确保您的帐户有权访问。
而且,这整个脚本是非常不安全的。 使用准备/参数化查询来避免SQL注入攻击。 现在处理数据的方式可能根本就不起作用。 addslashes()
是不够的。
这个错误的含义正是它所说的。 这不是你的PHP文件的问题 - 而是你实际上没有权限连接到你正在尝试的MySQL服务器。
root
用户身份连接到它吗? 这就是说,你的PHP文件也有很多问题。 最明显的就是你正在使用mysql_*
函数。 这些不应该在新代码中使用,因为它们不再被维护并且被正式弃用。 (看到红色框 ?)
您应该使用PDO或MySQLi。
第二个问题是你非常容易受到SQL注入攻击。 这可以通过对查询中包含的每个变量调用mysql_escape_string()
来解决,但更好的解决方案是切换到PDO或MySQLi并调查预准备语句。