-4

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\ramya\store.php on line 37 Could not connect localhost:Access denied for user 'root'@'localhost' (using password: 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();

?>
Brad
  • 159,648
  • 54
  • 349
  • 530
Ramya
  • 3
  • 2

3 Answers3

1

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
0

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.

  1. Is your MySQL server really hosted on localhost?
  2. Do you want to connect to it as the root user?
  3. Do you really mean to use a single space as the password?

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.

Community
  • 1
  • 1
jcsanyi
  • 8,133
  • 2
  • 29
  • 52
0

please check there is space in password, try removing that

$s_password = "";
PravinS
  • 2,640
  • 3
  • 21
  • 25