不确定为什么帖子没有插入我的数据库

时间:2013-07-15 14:12:18

标签: php mysql insert

我已经完成了我能想到的一切,但无法弄清楚为什么我的代码正在创建错误消息而不是插入数据库,我甚至把echo“
选择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'; ?>

2 个答案:

答案 0 :(得分:0)

在$ sql2的末尾有一个缺少的右括号。好吧,它适用于所有人。

答案 1 :(得分:0)

在您丢失的VALUES声明结束时,您只有“;目前。