无法使用DB_NAME:拒绝用户访问

时间:2017-02-18 06:42:08

标签: php html mysql html5 forms

我正在尝试创建一个联系表单并连接到数据库和错误 继续告诉我,我无法访问数据库中的表 我一直在争吵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();
?>

1 个答案:

答案 0 :(得分:0)

试试这个

<?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>