Karolis
Karolis

Reputation: 137

Insert form data into the database

Im trying to make a simple contact form and insert data to the database. I know that there are a lot of questions very similar to this but I just can't find out what is wrong with my code.
When I fill all form fields and try to submit, I always get that !mysqli_query($con, $sql) == FALSE. In other words, I can't insert any data into the database.

<?php 

if(isset($_GET['send'])){

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

    if(isset($name) && isset($email) && isset($phone) && isset($message) && !empty($name) && !empty($email) && !empty($phone) && !empty($message)){
        echo '<div class="alert alert-success" role="alert"><p class="alert">Your message was sent successfully.</p></div>';

    $con = mysqli_connect('localhost', 'root', '') or die('Cannot connect to database.');

    if(!mysqli_select_db($con, 'users')){
        echo 'Database is not choosen.';
    }

    $sql = "INSERT INTO person(Name, Email, Phone, Message) VALUES ('$name', '$email', '$phone', '$message')";

        if(!mysqli_query($con, $sql)){
            echo "Data is not inserted.";
        }
    } 

    else{
        echo '<div class="alert alert-danger" role="alert"><p class="alert">Some fields are empty.</p></div>';
    }
}

?>

<form action = "contact.php" method="get">
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Name" name="name">
    </div>
    <div class="form-group">
        <input type="email" class="form-control"  placeholder="Email" name="email">
    </div>
    <div class="form-group">
        <input type="number_format" class="form-control"  placeholder="Phone" name="phone">
    </div>
    <div class="form-group">
        <textarea class="form-control" rows="8" placeholder="Message" name="message"></textarea>
    </div>
    <div class="form-group">
        <button class = "btn btn-primary" style="width: 100%; height: 70px" type="submit" name="send"><span class="glyphicon glyphicon-send"></span></button>
    </div>
</form>

Upvotes: 0

Views: 97

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Nota: Posting as a community wiki. Not because I don't feel confident about the answer, but because I voted to close the question already, in all honesty of course ;-)

type="number_format" that is an invalid type.

Either use type="number" if HTML5 is available for you to use and is indeed an integer, or as type="text" to make it cross-browser compliant. Either way, your MySQL will process it.

That is the reason why your code is failing you here.

Consult the following on valid input types:

Plus, as a bonus:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.


Consult these following links http://php.net/manual/en/mysqli.error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code.

You should also check for empty fields, rather than using isset():

Otherwise, you may have errors/problems; that's if you're checking/looking out for them.

Upvotes: 1

Related Questions