简体   繁体   English

将表单数据插入数据库

[英]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 . 当我填写所有表单字段并尝试提交时,总是得到!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>

Nota: Posting as a community wiki. 通知:作为社区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. type="number_format"是无效的类型。

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. 如果HTML5可供您使用并且确实是整数,则使用type="number" ,或者使用type="text"以使其跨浏览器兼容。 Either way, your MySQL will process it. 无论哪种方式,您的MySQL都会对其进行处理。

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 . 您当前的代码可以进行SQL注入 Use prepared statements , or PDO with prepared statements . 使用准备好的语句 ,或使用带有准备好的语句的 PDO


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. 请参考以下链接http://php.net/manual/en/mysqli.error.phphttp://php.net/manual/en/function.error-reporting.php,并将其应用于您的代码。

You should also check for empty fields, rather than using isset() : 您还应该检查空字段,而不是使用isset()

Otherwise, you may have errors/problems; 否则,您可能会遇到错误/问题; that's if you're checking/looking out for them. 那是,如果你正在检查/望着他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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