首页 > 解决方案 > 将 HTML 表单连接到 SQL 数据库

问题描述

我使用 Elementor 上的 HTML 小部件在我的网站上创建了一个 HTML 表单。php 代码位于我的文件管理器中名为 config.php 的文件中。我正在尝试将 html 提交数据与 SQL 数据库连接,但无法获得连接。下面是我的 HTML 代码和 PHP 代码。我认为我最挣扎的地方是如何处理 html 表单,知道将数据发送到哪个文件以进行数据库连接和数据传输。任何帮助将不胜感激。

HTML 代码

<!DOCTYPE html>
    <html>
        <body>
            <h1>Tournament Registration</h1>
            <hr style=
            "background-color:black;
            margin:auto;">
            <form 
                action="/domains/XYZ.com/public_html/Forms/config.php"
                method="post">
            <div
                class="form-field">
                 <input 
                    style= 
                    "width:250px;
                    padding:10px;
                    margin-top: 20px;"
                    type=text
                    class=text
                    name=firstname
                    placeholder="First Name"
                    required>
                    <br>
                    <br>
            </div>
            <div
                class="form-field">
                <input 
                    style= 
                    "width:250px;
                    padding:10px;"
                    type=text
                    class=text
                    name=lastname
                    placeholder="Last Name"
                    required>
                    <br>
                    <br>
            </div>
            <div 
                class="form-field">
                <input 
                    style= 
                    "width:250px;
                    padding:10px;"
                    type=email
                    class=text
                    name=email
                    placeholder="Email"
                    required>
                    <br>
                    <br>
            </div>
            <div 
                class="form-field">
                <input 
                    style=
                    "width:250px;
                    padding:10px;"
                    type=text
                    class=text
                    name=phonenumber
                    placeholder="Phone Number"
                    required>
                    <br>
                    <br>
            </div>
            <div 
                class="form-field">
                <input 
                  style= 
                  "width:250px;
                  padding:10px;"
                  type=text
                  class=text
                  name="handicap"
                  placeholder="Handicap"
                  required>
                  <br>
                  <br>
            </div>
            <div 
                class="form-field">
                <select
                  style=
                  "width:250px;
                  padding:10px;"
                  class=text
                  name="shirtsize"
                  size = 1
                  required>
                    <option
                        value="" 
                        selected>
                        Shirt Size
                    </option>
                    <option
                        value="S">
                        S 
                    </option>
                    <option
                        value="M">
                        M 
                    </option>
                    <option
                        value="L"> 
                        L 
                    </option>
                    <option
                        value="XL">
                        XL 
                    </option>
                    <option
                        value="2XL">
                        2XL
                    </option>
                    <option
                        value="3XL">
                        3XL
                    </option>
                </select>
            </div>
            <br>
            <br>
            <div
                class="form-field">
                <input
                    style=
                    "width:250px;
                    padding:10px;
                    margin-left:auto;"
                    type=submit
                    value=Register>
            </div>
            </form>
        </body>
    </html>

PHP 代码

<?php
$servename = "localhost";
$database = "u759373654_Forms";
$username = "u759373654_username";
$password = "password";

// Create Connection

$conn = 
new msqli_connect($servername, $username, $password, $database);

// Check Connection

if  (!$conn) {
        die("Connection failed: ". msqli_connect_error());
}
echo "Connection Successful";

$firstname = $_POST[firstname];
$lastname = $_POST[lastname];
$email = $_POST[email];
$phonenumber = $_POST[phonenumber];
$handicap = $_POST[handicaop];
$shirtsize = $_POST[shirtsize];

$sql = "INSERT INTO Registration Form (firstname, lastname, email, phonenumber, handicap, shirtsize) VALUES('$firstname', '$lastname', '$email', '$phonenumber', '$handicap', '$shirtsize')";
if (mysqli_query($conn, $sql)) {
      echo "Registration Successful";
} else {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

标签: phphtmlsql

解决方案


推荐阅读