php - 将 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);
?>
解决方案
推荐阅读
- python - 获取在按数据框分组的多索引上具有最大列值的索引
- r - AzureCognitive R 包翻译服务返回 http 400:“请求正文不是有效的 JSON。”
- jenkins - 在共享机器上运行的 Jenkins 管道
- c - f = NAN 可能导致引发浮点异常是否被认为是正常的?
- mongodb - 在 Scala 中计算 Mongo 文档中单词的出现次数
- javascript - 使用 useRef [动画] 访问 Dom 元素
- python - 解决当月的最后一天python
- join - U-sql Inner JOIN 耗时太长
- docker - Docker 将只读挂载点挂载为卷
- bash - Azure DevOps Pipelines 中的 bash 脚本任务如何访问服务连接?