简体   繁体   中英

How do I submit HTML form data to MySql Database?

I have 2 php files...one with a html form and the other called contact_form.php. I keep getting this error every time i hit the submit button:

Parse error: syntax error, unexpected '}' in /contact_form.php on line 24

...and no data gets saved to my database...can anyone help me to get this code working...Thanks

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

You have a lot of code similar to this:

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

$name is an assignation of a php variable, you need to put a semicolon after you store the value inside the variable, like:

$name = $_POST['fullname'];

Do that on all your variables and retry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM