0
我试图做一个帖子和评论部分,用户可以发布和comment.For为此我有2个表:帖子,评论如何获得后的电流id,同时评论给它
在我帖子表中有两列:id和帖子;它是这样的:
id | post
----------------------
1 | lo;l;l
-----------------------
2 | i am feeling well
在我的评语表
有三列:ID,评论和POST_ID(这也正是用户评论的帖子的ID,这样我可以动态地检索评论每次发帖)
我管理的帖子部分,但无法弄清楚如何在评论时获取特定帖子的ID。 我不想要任何直接的解决方案,而是PDO的工作大纲如何做到这一点。在这方面的任何帮助将不胜感激
这是我迄今为止管理的(它可能有点长,但它的理解非常简单。我不提供我的数据库查询页面,以保持帖子小。所有我需要知道如何获得特定的post_id,而我正在评论它)
我还没有写在ajax调用内的insertComment.php页面,但我想我的评论插入和检索功能去那里
<?php
session_start();
require_once 'myDB.php';
if(isset($_POST['post']) && !empty($_POST['post'])){
$post=$_POST['post'];
$_SESSION['post']=$_POST['post'];
try{
$newComment=DB::getInstance();
$newComment->insert('post',array('posts'=>$post));
}catch(Exception $e){
echo $e->getMessage();
}
header('HTTP/1.1 303 see other');
header('Location:'.$_SERVER['PHP_SELF']);
}
?>
<html>
<head>
<style>
#formdiv{width:347px;height:120px;background:#dfe3ee;position:relative;border:1px dashed black;top:0px;left:300px;padding:5px; margin-bottom:3px;
}
#cmntbox{
width:347px;background:#dfe3ee;position:relative;border:1px solid black;left:300px;padding:5px;
}
.repText{
width:100%;background:#f7f7f7;position:relative;border:1px solid black;padding:3px;resize:none;}
}
</style>
</head>
</body>
<div id='formdiv'>
<form action='' method='POST'>
<textarea name='post' placeholder="what's on your mind !" cols='40' rows='3'></textarea>
<input type='submit' name='submit' value='post comment' style='float:right;width:100px;height:30px;background:#3b5998;color:white;'>
</form>
</div>
<?php
$newComment=DB::getInstance();
$results=$newComment->getComment('SELECT','post','posts')->result();
foreach($results as $result=>$val){
?>
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';?>
<form>
<textarea name='myrep' id='myreply' class='repText'></textarea>
<input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' >
</form>
</div>
<?php
}
?>
<script>
var reply=document.getElementsByClassName('reply');
var repText=document.getElementsByClassName('repText');
for(i=0;i<reply.length;i++){
(function(i){
reply[i].addEventListener('click',function(e){
var xmlHttp=new XMLHttpRequest();
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
//do nothing
}else{
alert('there was a problem ');
}
}
var parameters='myrep='+document.getElementById('myreq').value
xmlHttp.open("POST", "insertcomment.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(parameters);
}
});
})(i);
}
</script>
</body>
</html>
沿着这些线必须有一千个教程 – Strawberry
任何人都可以给我一些暗示..我完全在黑暗中? –
那么,通常您在选择帖子时选择post_id,并将其保存在表单中的隐藏输入内。 – Strawberry