如何将HTML表单数据提交到MySql数据库?

时间:2014-11-12 16:16:32

标签: php html mysql database forms

我有2个php文件...一个用html表单,另一个用contact_form.php。每次点击提交按钮时我都会收到此错误:

解析错误:语法错误,意外'}'在第24行的/contact_form.php中

...并且没有数据保存到我的数据库......任何人都可以帮助我使这段代码正常工作......谢谢

FORM

<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';

if($_POST['fullname']){
    $name = $_POST['fullname']
}else{
    echo "name not received";
    exit;
}
if($_POST['phonenumber']){
    $phone = $_POST['phonenumber']
}else{
    echo "phone not received";
    exit;
}
if($_POST['emailaddress']){
    $email = $_POST['emailaddress']
}else{
    echo "email not received";
    exit;
}
if($_POST['message']){
    $msg = $_POST['message']
}else{
    echo "message not received";
    exit;
}

$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));
}
?>

DATABASE SCREENSHOTS

http://oi58.tinypic.com/izq8na.jpg

http://oi57.tinypic.com/11hdz07.jpg

1 个答案:

答案 0 :(得分:0)

你有很多类似的代码:

if($_POST['fullname']){
    $name = $_POST['fullname']
}else{
    echo "name not received";
    exit;
}

$ name是一个php变量的赋值,你需要在将值存储在变量中之后放一个分号,如:

$name = $_POST['fullname'];

对所有变量执行此操作并重试。