-4

警告:mysql_connect():第 37 行 C:\xampp\htdocs\ramya\store.php 中的用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)无法连接 localhost:用户'root 的访问被拒绝'@'localhost'(使用密码:YES)

<?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();

?>
4

3 回答 3

1

此特定错误与您的代码无关。您一开始并没有登录 MySQL。确保您的帐户有权访问。

此外,整个脚本非常不安全。使用准备好的/参数化查询来避免 SQL 注入攻击。您现在处理数据的方式可能根本行不通。 addslashes()是不够的。

于 2013-07-04T05:08:25.410 回答
0

错误的意思正是它所说的。这不是您的 PHP 文件的问题 - 您实际上没有权限连接到您尝试连接的 MySQL 服务器。

  1. 您的 MySQL 服务器真的托管在 localhost 上吗?
  2. 您想以root用户身份连接到它吗?
  3. 您真的要使用单个空格作为密码吗?

话虽如此,您的 PHP 文件也存在许多问题。最明显的是您正在使用这些mysql_*功能。这些不应该在新代码中使用,因为它们不再被维护并且被正式弃用。(看到红框了吗?)

您应该改用PDOMySQLi

第二个问题是您极易受到 SQL 注入的攻击。这可以通过调用mysql_escape_string()查询中包含的每个变量来解决,但更好的解决方案是切换到 PDO 或 MySQLi 并调查准备好的语句

于 2013-07-04T05:08:51.740 回答
0

请检查密码中是否有空格,请尝试将其删除

$s_password = "";
于 2013-07-04T05:10:02.707 回答