php5将数据插入mysql

时间:2016-06-26 00:44:33

标签: sql database mysqli php-5.3

使用PHP5和MySQL,我正在制作一个简单的评论区域,用户可以在其中发表评论和留言。我想将用户名,电子邮件和消息插入数据库。保存PHP文件时页面上没有错误,但数据没有插入数据库。

<?php
error_reporting(0);
include 'db.php';
if ($conn) {
    $time   = time();
    $errors = array();
    if (isset($_POST['submit'])) {
        $name  = mysql_real_escape_string(htmlentities($_POST['guestbook_name']));
        $email = mysql_real_escape_string(htmlentities($_POST['guestbook_email']));
        $name  = mysql_real_escape_string(htmlentities($_POST['guestbook_message']));

        if (empty($name) || empty($email) || empty($name)) {
            $errors[] = "All feilds are required";
        }

        if (empty($errors)) {
            $sql = "INSERT INTO entries(date,name,email,message) VALUES($time, $name, $email, $message)";
            if (mysqli_query($conn, $sql)) {

                header("location: $_SERVER[PHP_SELF]");
            } else {
                $errors[] = "Something went wrong";
            }
        } else {

            foreach ($errors as $error) {
                echo "<div class='alert alert-danger'>$error</div>";
            }
        }

    }
} else {
    echo "Could not connect";
}
?>

的index.php

<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <div class="form-group">
        <label for="name">Your Name</label>
        <input type="text" class="form-control" id="name" name="guestbook_name">
    </div>

    <div class="form-group">
        <label for="email">Your Email</label>
        <input type="email" class="form-control" id="email" name="guestbook_email">
    </div>

    <div class="form-group">
        <label for="email">Your Message</label>
        <textarea class="form-control" rows="5" name="guestbook_message"></textarea>
    </div>
    <input type="submit" class="btn btn-success" value="post" name="submit">
</form>

数据库文件

<?php
$server    = 'localhost';
$user      = 'faizy';
$password1 = 'faizy';
$db        = 'guestbook';

$conn = mysqli_connect($server, $user, $password1, $db);

if (!$conn) {
    die("connection failed!:" . mysqli_connect_error());
}
?>

[在此处输入图像说明] [1]

  

错误   警告:htmlentities()要求参数1为字符串,对象在第9行的C:\ xampp \ htdocs \ demo \ phppractice \ index.php中给出

警告:mysqli_real_escape_string()正好需要2个参数,1在第9行的C:\ xampp \ htdocs \ demo \ phppractice \ index.php中给出

警告:htmlentities()要求参数1为字符串,对象在第10行的C:\ xampp \ htdocs \ demo \ phppractice \ index.php中给出

警告:mysqli_real_escape_string()需要2个参数,1在第10行的C:\ xampp \ htdocs \ demo \ phppractice \ index.php中给出

警告:htmlentities()要求参数1为字符串,对象在第11行的C:\ xampp \ htdocs \ demo \ phppractice \ index.php中给出

警告:mysqli_real_escape_string()需要2个参数,1在第11行的C:\ xampp \ htdocs \ demo \ phppractice \ index.php中给出

0 个答案:

没有答案