我已经检查了我能想到的所有内容,但无法弄清楚为什么我的代码会创建错误消息并且没有插入数据库,我什至输入了 echo "
Select SQL = ".$sql2."
"; 在我的结果查询并获得以下数据之前...
Select SQL = INSERT INTO `posts`(post_topic, post_id, post_content, post_date, post_by)VALUES('3', '2', 'me too', '15/07/13 22:04:00', '1'
ERROR
检查值,一切正常,帖子加入的主题是3,它是帖子表中的第二个帖子,“我也是”是正确的帖子内容,数据很好,会话user_id为1,我我不知所措,这是该页面的完整代码。
<?php
include 'core/init.php';
include 'includes/overall/header.php';
// Get value of id that sent from hidden field
$id=$_POST['id'];
// Find highest answer number.
$sql="SELECT MAX(post_id) AS Maxa_id FROM `posts` WHERE post_topic='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}
// get values that sent from form
$post_content=$_POST['post_content'];
$datetime=date("d/m/y H:i:s"); // create date and time
// Insert answer
$sql2="INSERT INTO `posts`(post_topic, post_id, post_content, post_date, post_by)VALUES('$id', '$Max_id', '$post_content', '$datetime', '" . $_SESSION['user_id'] . "'";
echo "<BR>Select SQL = ".$sql2."<BR>";
$result2=mysql_query($sql2);
if($result2){
echo "Successful<BR>";
echo "<a href='view_topic.php?topic_id=".$id."'>View your answer</a>";
// If added new answer, add value +1 in reply column
$sql3="UPDATE `topics` SET reply='$Max_id' WHERE topic_id='$id'";
echo "<BR>Select SQL = ".$sql3."<BR>";
$result3=mysql_query($sql3);
}
else {
echo "ERROR";
}
// Close connection
mysql_close();
?>
<?php include 'includes/overall/footer.php'; ?>