簡體   English   中英

無法連接到數據庫以更新成員詳細信息

[英]Can't connect to database to update member details

在提交更新的詳細信息時運行此頁面時,我無法連接到我的數據庫。 對於我的生活無法弄清楚為什么它不會連接。

最近的錯誤:

警告: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!"; 

  }
?>

替換這個:

 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 ,則不知道錯誤是什么,只是字符串。

哈哈對不起,但我看到了你的問題。 你覆蓋你的用戶名:)

$username = "root";

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

因此該用戶的錯誤:

用戶“MattS1”@“localhost”拒絕訪問

而不是用戶“root”。

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM