将PHP表单发送到MySQL数据库

时间:2018-04-25 03:25:05

标签: php mysql

我是PHP的新手,我真的很难将我的PHP联系表单数据发布到我的数据库(MySQL,MAMP,phpMyAdmin),我不知道为什么我发现这么复杂,我一直在查看我的代码但无法弄清楚。 my database structure

connection.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();
?>

contact.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>

3 个答案:

答案 0 :(得分:0)

尝试用这样的双引号包装您的参数

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

我建议您将参数绑定到bind_param以避免sql注入

答案 1 :(得分:0)

我已从表单的第一行删除(name =&#34; sentMessage&#34; id =&#34; contactForm&#34;)并且一切正常??

原始行(有错误)

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

已编辑的行(已删除的错误)

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

答案 2 :(得分:0)

作为其他帖子的补充,请尝试使用预备语句。这比使用mysqli函数更安全。 W3schools是一个学习的好地方!他们有一些关于这个主题的文章。 https://www.w3schools.com/php/php_mysql_prepared_statements.asp

祝你好运!