PHP不向Xampp数据库发送数据

时间:2018-11-29 22:15:39

标签: php mysql forms function xampp

我正在遵循一个教程系列(Link to Video),其中我正在学习如何创建网页的注释部分。我正在使用XAMPP,因为视频中的人正在使用XAMPP。我已经完成了将数据(名称,时间,消息)发送到数据库的代码,当我尝试尝试时,什么也没有发生。我检查了数据库,什么都没有

这是代码:

index.php

<?php

date_default_timezone_set('Europe/London');
include 'dbh.inc.php';
include 'comments.inc.php';
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<?php
echo"<form method='POST' action='".setComments($conn)."'>
    <input type='hidden' name='uid' value='Anonymous'>
    <input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
    <textarea name='message'></textarea> <br>
    <button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>

</body>
</html>

comments.inc.php

<?php

function setComments($conn) {
    if (isset($_POST['commentSubmit'])) {
        $uid = $_POST['uid'];
        $date = $_POST['date'];
        $message = $_POST['message'];

        $sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
        $result = mysql_query(- $sql);
    }   
}

dbh.inc.php

<?php

$conn = mysqli_connect('localhost', 'root', '', 'commentsection');

if (!$conn) {
    die("Connection Faild: ".mysql_connect_error());
}

请帮助我。 谢谢

1 个答案:

答案 0 :(得分:-2)

更改

<?php
echo"<form method='POST' action='".setComments($conn)."'>
    <input type='hidden' name='uid' value='Anonymous'>
    <input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
    <textarea name='message'></textarea> <br>
    <button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>

收件人:

<?php
// action empty send the post data to the this fila again
// setComments function have a condition to work only when POST data is present
setComments($conn);
echo"<form method='POST' action=''>
    <input type='hidden' name='uid' value='Anonymous'>
    <input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
    <textarea name='message'></textarea> <br>
    <button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>

此:

    $sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
    $result = mysql_query(- $sql);
}   

收件人:

    // INSERT is the correct sintaxis
    $sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
    $result = mysql_query($sql);
}   

最后

$conn = mysqli_connect('localhost', 'root', '', 'commentsection');

收件人:

// mysqli_connect have diferent parameters
$conn = mysql_connect('localhost', 'root', '', 'commentsection');

经过测试,可以正常工作