Php 如何在评论文章时获取文章的当前id
我正在尝试创建一个post和comment部分,用户可以在其中发布和评论。为此,我有两个表:posts,comment 在我的帖子表中有两列:id和posts;事情是这样的:Php 如何在评论文章时获取文章的当前id,php,mysql,ajax,Php,Mysql,Ajax,我正在尝试创建一个post和comment部分,用户可以在其中发布和评论。为此,我有两个表:posts,comment 在我的帖子表中有两列:id和posts;事情是这样的: id | post ---------------------- 1 | lo;l;l ----------------------- 2 | i am feeling well 在我的评论表中有三列:id、comment和post_id(,这是用户发表评论的帖子的id,因此每次我都可以动
id | post
----------------------
1 | lo;l;l
-----------------------
2 | i am feeling well
在我的评论表中有三列:id、comment和post_id(,这是用户发表评论的帖子的id,因此每次我都可以动态检索每篇帖子的评论
我管理了post部分,但在评论时不知道如何获取特定post的id我不想要任何直接的解决方案,而是要一份PDO中关于如何做到这一点的工作大纲。在此方面的任何帮助都将不胜感激
这就是我到目前为止所做的(可能有点长,但理解起来很简单。我提供数据库查询页面并不是为了让帖子变小。我只需要知道如何在评论时获得特定的帖子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>
#formdiv{宽度:347px;高度:120px;背景:#dfe3ee;位置:相对;边框:1px黑色虚线;顶部:0px;左侧:300px;填充:5px;边距底部:3px;
}
#cmntbox{
宽度:347px;背景:#dfe3ee;位置:相对;边框:1px纯黑色;左侧:300px;填充:5px;
}
雷普特先生{
宽度:100%;背景:#F7F7;位置:相对;边框:1px纯黑色;填充:3px;调整大小:无;}
}
var reply=document.getElementsByClassName('reply');
var repText=document.getElementsByClassName('repText');
对于(i=0;i,在加载页面时,您将拥有帖子id,因此在html中绘制评论框时,您可以将帖子id设置为某个html元素的属性或隐藏字段
因此,在您的情况下,请更新以下脚本
<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>
到
这条线上肯定有上千个教程,有人给我一些提示吗?我完全不知道这里是什么?好吧,通常你在选择帖子时会选择帖子id,并将其保存在表单中的隐藏输入中。是的,那很好。什么是合适的sql查询…?我很难做到这一点,,,,以及所有关于sele的内容点击帖子?如何选择帖子和sql查询?你能更详细一点吗?你是在使用Wordpress codex吗?
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';?>
<form id="<?php echo $val->postId ?>">
<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>
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';?>
<form>
<input type="hidden" name="postId" value="<?php echo $val->postId ?>" />
<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>