简体   繁体   中英

Could not update data: Access denied for user 'xxxx'@'localhost' (using password: NO)

Okay so I have been trying to fix this issue for a few hours now and have had no luck. Hopefully one/some of you brilliant programmers can aid in getting this code working properly. Currently, I have two databases set up: Users and AvatarDB. In Users, I have a table called userinfo and in AvatarDB I have a table called Avatars. What I am trying to do is have Users upload an avatar which will be stored in the AvatarDB and will have the same ID as the user ID. (For simplicity and troubleshooting purposes, I am currently just asking the user to enter their ID into the form, however once I get this code working will then have the code retrieve their user ID from my Users database) Unfortunately, when I try to upload an avatar I get the error in the title. Here is my code for connecting to the database:

$servername = "localhost";
$username = "xxxx"; (I put the username here)
$password = "xxxx"; (I put the password here)

// Create connection
$db = mysql_connect($servername, $username, $password);

// Check connection
if (!$db) { die("Connection failed: " . mysql_connect_error()); }

Then, my upload.php file is as follows:

<?PHP include(connect.php) ?>

<form id="avatar" name="avatar" method="post" action="submitAvatar.php" enctype="multipart/form-data">

ID:
<input type="text" id="memberId" name="memberId">

Upload Avatar:
<input type="file" onchange="load_image(this.id,this.value);" id="userAvatar" name="userAvatar"> 

<br><br>
<input type="submit" value="Update Avatar" id="updateAvatar" name="updateAvatar">

</form>

My submitAvatar.php is as follows:

<?PHP include(connect.php);

if($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['updateAvatar'] == 'Update Avatar') {
    if($_POST['memberId']=="") {
        echo "Member id is required";
    } else {

        $memberId = $_POST['memberId'];
        $updtDate = date("Y-m-d : H:i:s", time());
        $allowedExts = array("gif", "jpeg", "jpg", "png", "bmp");
        $temp = explode(".", $_FILES["userAvatar"]["name"]);

        if($_FILES['userAvatar']['size']>0) {
            if (in_array(strtolower($temp[1]), $allowedExts)) {

                $picNm = uniqid().".jpg";
                $path="memberAvatars/".$picNm;

                if(move_uploaded_file($_FILES['userAvatar']['tmp_name'],$path)) {

                    $sql = "UPDATE Avatars ". "SET name = $picNm ". "WHERE memberId = $memberId" ;
                    mysql_select_db('AvatarDB');
                    $retval = mysql_query( $sql, $db );
                    if(! $retval ) { die('Could not update data: ' . mysql_error()); }
                    echo "Updated data successfully\n";
                    mysql_close($db);
                } else {
                    echo "Error1.";
                }
            } else {
                echo "Invalid type.";
            }
        }
    }
}
?>

Anyone have any idea why I'm getting the following error?

Could not update data: Access denied for user 'xxxx'@'localhost' (using password: NO)

Thank you in advance!

Here are my ideas on this issue. No need to put <?PHP include(connect.php) ?> in upload.php unless your actually doing a query there which it doesn't look like. It's already included in submitAvatar.php .

I believe the issue is in the syntax of your query or the way you have included your connection script. You may need to use quotes inside your include.

<?PHP include('connect.php') ?>

Also, try changing

$sql = "UPDATE Avatars ". "SET name = $picNm ". "WHERE memberId = $memberId" ;

to

$sql = "UPDATE Avatars SET name = $picNm WHERE memberId = $memberId";

Alternatively, if you want it quoted, the correct format would be.

$sql = "UPDATE Avatars SET name = '".$picNm."' WHERE memberId = '".$memberId"'";

EDIT:

After looking more closely at your code, I believe the problem lies here:

mysql_select_db('AvatarDB');
$retval = mysql_query( $sql, $db );

Looks like you've got them switched, try

mysql_select_db('AvatarDB', $db);
$retval = mysql_query($sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM