php不向xampp数据库发送数据

xqnpmsa8  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(387)

这个问题在这里已经有答案了

为什么我不应该在php中使用mysql函数(12个答案)
去年关门了。
我遵循一个教程系列(视频链接),其中我学习创建一个网页的评论部分。我使用的是xampp,因为这是视频中的家伙正在使用的。我已经完成了将数据(名称、时间、消息)发送到数据库的代码的编写,当我去尝试时什么都没有发生。我查了数据库什么都没有
代码如下:
索引.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>

注解.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());
}

请帮帮我。谢谢您

c0vxltue

c0vxltue1#

改变

<?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');

测试和工作

相关问题