无法将数据插入MySQL数据库,即使连接似乎没问题,我也没有从我的页面或服务器收到任何错误,并且我已经验证了我的数据库凭据是否正确。评论根本没有出现在数据库中,当我点击提交时基本上消失了。我一直在努力,但我仍然感到困惑。如果有人可以看看,将非常感激。
<?php
date_default_timezone_set('UTC');
include '../conn/dba.inc.php';
include '../function/comments.inc.php';
?>
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body onLoad="document.forms.Mfrm.message.focus()">
<?php
echo
"<form method='POST' action='".setComments($conn)."' name='Mfrm'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:m')."'>
<input type='text' class='inptcss' name='message' placeholder='Placeholder'/>
<button type='submit' name='commentSubmit'>Comment</button>
</form><br>";
?>
</body>
</html>
comments.inc.php
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])) {
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (uid,date,message)
VALUES ('$uid', '$date', '$message')";
$result = $conn->query($sql);
}
}
?>
dba.inc.php(当我运行这个时,我得到了响应&#34;数据库被选中&#34;)
<?php
$servername = "localhost";
$username = "123456789_username";
$password = "dbpassword";
$dbname = "123456789_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
if(!$conn)
{echo'Not Connected To Server';}
if(!mysqli_select_db($conn,'$dbname'))
{echo'Database Is Not Selected';}
if(mysqli_select_db($conn,'$dbname'))
{echo'Database is Selected';}
?>
以下是用于在我的数据库中创建表的代码:
CREATE TABLE comments (
cid int(11) not null AUTO_INCREMENT PRIMARY KEY,
uid varchar(128) not null,
date datetime not null,
message TEXT not null);
提前谢谢!