在数据库中插入注释,并使用匹配的ID和ad(php,mysql)


Insert comment to database with the right ID to match and ad (php , mysql)

我想让你可以在买卖网站上评论广告。

在主页上,我显示了所有的广告(所有的广告都有一个名为AdID的id),在每个广告下面我都有一条评论区。

我的问题是,我似乎无法将我的AdID从正确的广告传递到我进行INSERT语句的if语句。

我可以通过在另一个页面上设置评论部分并用$_GET传递AdID来解决这个问题,但我真的希望人们能够在主页上的广告下进行评论。

如果您需要代码的其余部分,我们将不胜感激。

    $res2 = $mysqli->query($query) or die("Could not query database" . $mysqli->errno . " : " . $mysqli->error); //Performs query 
     while($row2 = $res2->fetch_object()) {
    $Comment = utf8_decode(htmlspecialchars($row2->Comment));
    $AdID = utf8_decode(htmlspecialchars($row2->AdID));
    $UserID = utf8_decode(htmlspecialchars($row2->UserID));
    $Fname1 = utf8_decode(htmlspecialchars($row2->Fname));
    $Lname1 = utf8_decode(htmlspecialchars($row2->Lname));   
    $c .= <<<END
 <div class="CommentBox">
 {$Fname1} {$Lname1}
 {$Comment}
 </div>
 END;
 }
    if(!empty($_POST)) {

    $msg = utf8_encode($mysqli->real_escape_string($msg));  
    $UserID = isset($_SESSION["UserID"]) ? $_SESSION["UserID"] : "NULL";
    $query = <<<END
    INSERT INTO comment (Comment, UserID, AdID) 
    VALUES('{$msg}', '{$UserID}' , '{$AdID}' ); 
END;
    $res = $mysqli->query($query) or die("Could not query database" . $mysqli-    >errno . " : " . $mysqli->error); //Performs query 
}
$c .= <<<END
        <form action="comTest.php" method="post">
    <input type="text" id="stor" name="msg" value="{$msg}"   placeholder="Comment"/>
        </form>
END;

} 

您没有从表单中获取发布的msg值。您需要先获取它;

$msg = $_POST["msg"];

而且,把一个隐藏的元素形成,你可以得到广告ID像;

<form action="comTest.php" method="post">
    <input type="text" id="stor" name="msg" value="{$msg}"   placeholder="Comment"/>
    <input type="hidden" id="adId" name="adId" value="$AdID"/>
</form>