gpt4 book ai didi

php - 无法使用 DB_NAME : Access denied for user

转载 作者:行者123 更新时间:2023-11-29 19:23:19 25 4
gpt4 key购买 nike

我正在尝试制作联系表单并连接到数据库,但出现错误一直告诉我无法访问数据库中的表我已经挣扎了差不多7个小时了,我真的很累了有什么帮助吗? error image

table image

<?php
$DB_NAME = "form";
$DB_USER = "root";
$DB_PASSWORD = "";
$DB_HOST = "localhost";

$link = mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME);

if (!$link){
die("could not connect:" . mysql_error());
};


$db_selected = mysql_select_db($DB_NAME,$link);

if(!$db_selected) {
die("Can\'t use " . $DB_NAME . ' : ' . mysql_error());
};

$value = $_POST["user_name"]; // required
$value2 = $_POST["user_eamil"];// required
$value3 = $_POST["subject"];
$value4 = $_POST["text_massege"];// required



$sql = "INSERT INTO form (user_name , user_eamil , subject , text_massege) VALUES ('$value','$value2','$value3','$value4')";

if (!mysql_query($sql)){
die("Error: " . mysql_error());
};


// Build the email (replace the address in the $to section with your own)
$email_from = $value2;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $value.\n".
"Here is the message:\n $value4".

$to = "mail";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $value2 \r\n";


if (!preg_match("/^[a-zA-Z ]*$/",$value)) {
$nameErr = "Only letters and white space allowed";
}
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
mysql_close();
?>

最佳答案

试试这个

<?php
$DB_NAME = "form";//Database name
$DB_USER = "root";
$DB_PASSWORD = "";
$DB_HOST = "localhost";

$link = mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME);

if (!$link){
die("could not connect:" . mysql_error());
};


$db_selected = mysql_select_db($DB_NAME,$link);

if(!$db_selected) {
die("Can\'t use " . $DB_NAME . ' : ' . mysql_error());
};

if (isset($_POST["user_name"])) {
$value = $_POST["user_name"]; // required
$value2 = $_POST["user_eamil"];// required
$value3 = $_POST["subject"];
$value4 = $_POST["text_massege"];// required


//Please use table name here
$sql = "INSERT INTO form (user_name , user_eamil , subject , text_massege) VALUES ('$value','$value2','$value3','$value4')";

if (!mysql_query($sql)){
die("Error: " . mysql_error());
};


// Build the email (replace the address in the $to section with your own)
$email_from = $value2;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $value.\n".
"Here is the message:\n $value4".

$to = "mail";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $value2 \r\n";


if (!preg_match("/^[a-zA-Z ]*$/",$value)) {
$nameErr = "Only letters and white space allowed";
}
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
mysql_close();
}
?>

<form method="post">
<input type="text" name="user_name" >
<input type="text" name="user_eamil" >
<input type="text" name="subject" >
<input type="text" name="text_massege" >
<input type="submit" value="submit" >
</form>

关于php - 无法使用 DB_NAME : Access denied for user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42312013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com