简体   繁体   中英

Access denied for user ''@'localhost' to database 'stay'

This is my php code

<?php
if( $_POST )
{

$con = mysql_connect('localhost','','');

if (!$con)
{

die('Could not connect: ' . mysql_error());

}

$db_selected = mysql_select_db('stay', $con);

// escape variables for security

$firstname = mysql_real_escape_string($_POST['uname']);

$lastname = mysql_real_escape_string($_POST['uemail']);

$password = mysql_real_escape_string($_POST['upass']);

$rpassword = mysql_real_escape_string($_POST['urpass']);

echo $sql = "INSERT INTO reg(name,email,pass,rpass)VALUES ('$firstname', 
'$lastname', '$password','$rpassword')";

if (!$db_selected) 
{

    die ('Can\'t use stay : ' . mysql_error());

}

else
{

  mysql_query($sql,$con);

}

mysql_close($con);

}

?>

This is the screenshot

I want to perform insert operation using POST. The problem is of access to the database in phpMyAdmin named as stay. I am uploading the screen shot too.

Your user cannot be blank in row

$con = mysql_connect('localhost','','');

Check it out, probbably it's 'root' if you have a newly installed xampp/wampp server

  1. In your line #5 you should type correct database username and password:

     $con = mysql_connect('localhost','someusername','somepassword'); 
  2. Don't use any mysql_* functions. They're deprecated. Use MYSQLI or PDO instead.

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