无法连接到数据库以更新成员详细信息

时间:2014-04-06 16:35:33

标签: php mysql

在提交更新的详细信息时运行此页面时无法连接到我的数据库。因为我的生活无法弄清楚为什么它不会连接。

最近的错误:

  

警告:mysql_connect()[function.mysql-connect]:在第69行的F:\ xampp \ htdocs \ Site \ ProfileUpdate.php中拒绝用户'MattS1'@'localhost'(使用密码:YES)的访问权限不能连接

下面是db.inc文件的详细信息,因为它没有密码我不知道为什么会发布此错误。

我的db.inc文件是:

$hostname = "localhost";
$username = "root";
$password = "";
$databasename= "schurter_products";

PHP / SQL:

<?php
 session_start();
 require 'db.inc';


  // Initialise an error string
  $errorString = "";

  // trim the POSTed values - gets rid of unecessary whitespace 
   $firstname = trim($_POST["Firstname"]);
   $surname   = trim($_POST["Surname"]);
   $emailaddress   = trim($_POST["Emailaddress"]);
   $username    = trim($_POST["Username"]);
   $password    = trim($_POST["Password"]);
  //Here we use validation at the server
  // Vaildate the firstname

  if (empty($firstname)) 
      // First name cannot be a null string
      $errorString .=
          "\n<br>The first name field cannot be blank.";

  // Validate the Surname
  if (empty($surname))
      // the user's surname cannot be a null string
      $errorString .=
          "\n<br>The surname field cannot be blank.";

  // Validate the email Address
  if (empty($emailaddress))
      // the user's address cannot be a null string
      $errorString .=
          "\n<br>You must supply at least one address line.";

  // Validate the username
  if (empty($username))
      // the user's city cannot be a null string
      $errorString .= "\n<br>You must supply a city.";

  // Validate password
  if (empty($password))
      // the user's city cannot be a null string
      $errorString .= "\n<br>You must supply state.";


  // Now the script has finished the validation, 
  // check if there were any errors
  if (!empty($errorString))
  {
      // There are errors.  Show them and exit.
?>
<!DOCTYPE HTML PUBLIC 
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head><title>Customer Details Error</title></head>
<body bgcolor="white">  
<h1>Customer Details Error</h1>
<?=$errorString?>
<br><a href="updateInsertForm.php">Return to the customer form</a>
</body>
</html>
<?php     

  }
  else
  {
   // If we made it here, then the data is valid
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$databasename")or die("cannot select DB");

//  this is an update


       $query = "UPDATE members SET Firstname = '$firstname', Surname = '$surname', Emailadress = '$emailaddress', Username = '$username', Password = '$password' WHERE id = '$memberID'";


        $recordSet = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

        echo "Your updates are complete!"; 

  }
?>

3 个答案:

答案 0 :(得分:1)

替换这个:

 mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
 mysql_select_db("$databasename")or die("cannot select DB");

 mysql_connect($hostname, $username, $password)or die(mysql_error());
 mysql_select_db($databasename)or die(mysql_error());
  • 不要在变量附近使用引号。

  • 更好地使用mysql_error()进行更好的调试,以了解错误是什么。如果您打印cannot connect,您就不知道错误是什么,只是字符串。

答案 1 :(得分:1)

对不起,但是我看到了你的问题。您覆盖了用户名:)

$username = "root";

$username    = trim($_POST["Username"]);

因此该用户的错误:

  

拒绝用户'MattS1'@'localhost'

而不是用户“root”。

答案 2 :(得分:0)

mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$databasename")or die("cannot select DB");

您应该删除每个变量之间的语音标记,检查MySQL用户和MySQL数据库是否已连接并具有有效权限,还要检查MySQL用户密码是否正确以及用户名或数据库名称没有前缀。例如,如果您使用的是cPanel,而您的用户名是foo,那么它将是foo_root和foo_schurter_products

只是一个专业提示,MySQL在更高版本的PHP中被弃用,并且提示您使用MySQLi。