我会正确解决问题。我的代码中的一行似乎正在起作用。它返回else,并且似乎不想运行它。它没有错误,但它只是不会将它移动到数据库。正如标题所说,它是if语句中的mysqli_query。这是代码,我已经把箭头放到了错误的位置。
$t_name = $_POST['topic_name'];
$content = $_POST['con'];
$date = date("y-m-d");
if(isset($_POST['submit'])) {
if($t_name && $content) {
if(strlen($t_name) >= 10 && strlen($t_name) <= 70) {
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
if($query = mysqli_query($connect, "INSERT INTO topics('topic_id', 'topic_name', 'topic_content', 'topic_creator', 'date') VALUES ('', '".$t_name."', '".$content."', '".$_SESSION["username"]."', '".$date."')")) {
echo "<font color = 'green'>Topic posted!</font>";
} else {
echo "<font color='red'>Unknown failure, time to panic!</font>";
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} else {
echo "<font color='red'>Topic name must be between 10 and 70 characters long!</font>";
}
} else {
echo "<font color='red'>All fields must be filled out!</font>";
}
}
} else {
include('header.php');
echo "You must be logged in to access this page. Click <a href='login.php'>here</a> to login.";
}
include('footer.php');
是的,我知道SQL注入和其他事情的危险,一旦我的代码关闭,我将处理它。
答案 0 :(得分:3)
请删除每个列名称的引号:'topic_id','topic_name'....'date'
$query = mysqli_query($connect, "INSERT INTO topics(topic_id, topic_name, topic_content, topic_creator, date) VALUES ('', '".$t_name."', '".$content."', '".$_SESSION["username"]."', '".$date."')")
为了理解您的错误,我建议您调试查询。尝试使用mysqli_error() 函数。
if (...) {
} else {
die( mysqli_error($connect)); // Add this
}
答案 1 :(得分:2)
您的topic_id
不能设为null。而是删除topic_id
或为其赋予default
值。
此外,与@HizerCache一样,您可以使用MySQL函数NOW()
插入当前时间。
这样的事情:
"INSERT INTO topics(topic_id, topic_name, topic_content, topic_creator, date) VALUES (default, '".$t_name."', '".$content."', '".$_SESSION["username"]."', NOW())"
另外,使用mysqli_error()
来调试错误。
if($query = mysqli_query($connect, "INSERT INTO topics('topic_id', 'topic_name', 'topic_content', 'topic_creator', 'date') VALUES ('', '".$t_name."', '".$content."', '".$_SESSION["username"]."', '".$date."')")) {
echo "<font color = 'green'>Topic posted!</font>";
} else {
echo "Error : " . mysqli_error($connect);
}