php - 表单不会将数据提交到MySQL数据库

标签 php html mysql forms

我的html表单有1个文件,下面的代码有另一个php文件。单击提交按钮,我就能成功连接。但是,当我在phpMyAdmin控制面板中刷新数据库时。没有数据被推送到我的数据库。

HTML格式

<form class="well" id="contactForm" name="sendMsg" novalidate="" action="contact_form.php" method="post">
        <div class="control-group">
            <div class="controls">
                <input class="form-control" name="fullname" id="name" type="text" placeholder="Full Name" required="" data-validation-required-message="Please enter your name" />
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input class="form-control" name="phonenumber" id="phone" type="text" placeholder="Phone Number" required="" data-validation-required-message="Please enter your phone number" />
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input class="form-control" name="emailaddress" id="email-address" type="email" placeholder="Email Address" required="" data-validation-required-message="Please enter your email" />
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <textarea rows="6" class="form-control" name="message" id="msg" type="msg" placeholder="Enter detailed question/concern, and we will get back to you." required="" data-validation-required-message="Please enter your question/concern"></textarea>
            </div>
        </div>
        <div class="control-group">
            <div class="controls submit-btn">
                <button class="btn btn-primary" type="submit" value="Submit">Submit</button>
            </div>
        </div>
    </form>


CONTACT_FORM.PHP

<?php

define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}

$db_selected = mysqli_select_db($connection, DB_NAME);

if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}

echo 'Connected successfully';

$name = @$_POST ['fullname'];
$phone = @$_POST ['phonenumber'];
$email = @$_POST ['emailaddress'];
$msg = @$_POST ['message'];

$sql = "INSERT INTO contact_form (fullname, phonenumber, emailaddress, message) VALUES ('$name',   '$phone', '$email', '$msg')";

if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
?>

最佳答案

首先使用该构造:

if (isset($_POST['fullname'])) {
    $name = $_POST['fullname'];
}


对于其他值,依此类推。

还是更好:-

$name = isset($_POST['fullname']) ? $_POST['fullname'] : '';


这样,字段$name始终设置为某些内容,因此以后尝试在代码中使用它时不会出错

关于php - 表单不会将数据提交到MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878720/

相关文章:

php - 表名有下划线时的型号名称

html - 像 :active:hover. 这样组合事件和悬停伪选择器是否正确?

c# - 可以一列来另一个sql查询吗

mysql - Django 脚本运行每次迁移

php - html5获取文件路径

php - 获取分析 View (配置文件)的目标列表

php - 如果条件正确,如何插入数据

jquery - 在video.js播放器上暂停播放

javascript - 使用文件夹制作网站的通用页眉和页脚

mysql - phpMyadmin 无法将默认值设置为 Null