用php实现html表单到数据库的转换

uqxowvwt  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(270)

我一直在尝试将这个html表单与mysql数据库连接起来,这样我就可以从我的网站上获得发送给我的消息。但我一直收到这样的信息:
“未声明html文档的字符编码。如果文档包含美国ascii范围以外的字符,则在某些浏览器配置中,文档将以乱码文本呈现。页面的字符编码必须在文档或传输协议中声明。”
我试过很多不同的方法,但都不适合我。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="js/jquery.js"></script>
        <link rel="stylesheet" href='https://fonts.googleapis.com/css?family=ABeeZee'>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="js/navshrink.js"></script>
        <link rel="stylesheet" type="text/css" href="css/contact.css">
    </head>

 <div class="contactCon">
       <form class="contactForm" action="contactForm.php" method="post" enctype="application/x-www-form-urlencoded">
          <ul class="flex-container">
             <li class="flex-item">
                <ul class="flex-container2">
                    <li class="flex-item2">enter code here
                        <label for="fname">First Name:</label>
                        <input type="text" id="fname" name="firstname" placeholder="Your First Name..">
                    </li>
                    <li class="flex-item2">
                        <label for="lname">Last Name:</label>
                        <input type="text" id="lname" name="lastname" placeholder="Your Last Name..">
                    </li>
                    <li class="flex-item2">
                        <label for="email">E-mail:</label>
                        <input type="text" id="email" name="email" placeholder="Your E-mail..">
                    </li>
                </ul>
                <li class="flex-item">
                    <label id="subject-label" for="subject">Subject:</label>
                    <textarea type="text" id="subject" name="message" placeholder="Write Me Something.."></textarea>
                    <input id="submit-button" type="submit" value="Submit">
                </li>
             </li>
          </ul>
       </form>
    </div>

下面是我的php代码:

<?php
  // Create Variables for Database Information
  $host = 'xxxxxx';
  $userName = 'xxxxxxx';
  $dbPassword = 'xxxxxx';
  $dbName = 'xxxxxxxx';

  // Create Connection to the Database
  $con = mysqli_connect($host,$userName,$dbPassword,$dbName);

  // Check connection
  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
    // Create Variables for every Input
    $firstname = mysqli_real_escape_string($con, $_POST['firstname']);
    $lastname = mysqli_real_escape_string($con, $_POST['lastname']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $message = mysqli_real_escape_string($con, $_POST['message']);

    // Insert Input into the Database
    $sql = "INSERT INTO contactMe(firstname, lastname, email, message)
      VALUES ('$firstname','$lastname','$email','$message')";

    // Check for Errors
    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    // Close Connection
    mysqli_close($con);
?>

提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题