gpt4 book ai didi

php - 将 PHP 表单发送到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-30 21:44:33 25 4
gpt4 key购买 nike

我是 PHP 的新手,我真的很难将我的 PHP 联系表单数据发布到我的数据库(MySQL、MAMP、phpMyAdmin),我不知道为什么我发现它如此复杂,我已经查看我的代码很多年了但无法弄清楚。 my database structure

连接.php

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "FutureDesign";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
//if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
//}
//echo "Connected successfully";

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

$sql = "INSERT INTO ContactForm (name, phone, email, message ) VALUES ('$name', '$phone', '$email', '$message')";


if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

联系人.php

<form name="sentMessage" id="contactForm" method="post" action="connection.php" novalidate>
<div class="control-group form-group">
<div class="controls">
<label>Full Name:</label>
<input type="text" class="form-control" name="name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Phone Number:</label>
<input type="tel" class="form-control" name="phone" id="phone" required data-validation-required-message="Please enter your phone number.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Email Address:</label>
<input type="email" class="form-control" name="email" id="email" required data-validation-required-message="Please enter your email address.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Message:</label>
<textarea rows="10" cols="100" class="form-control" name="message" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
</div>
</div>
<div id="success"></div>
<!-- For success/fail messages -->
<button type="submit" value="submit" class="btn btn-primary" id="sendMessageButton">Send Message</button>
</form>

最佳答案

我已经从表单的第一行删除了 (name="sentMessage"id="contactForm") 并且一切正常?!

原行(有错误)

<form name="sentMessage" id="contactForm" method="post" action="connection.php" novalidate>

编辑行(删除错误)

<form method="post" action="connection.php" novalidate>

关于php - 将 PHP 表单发送到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50013589/

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