仅循环将数据插入一行

时间:2019-03-10 11:34:07

标签: php mysql sql

我有两个表(table_server)和(table_comment)。 当我将数据放入table_server循环内的table_comment中时,会将数据放入表的每一行中。

有没有一种方法可以仅基于row-parent_id在一行中插入数据? 因此,如果我在第4行中写评论,那么它只会在第4行中记录。

Example of my table_comment

echo "<ul>";
if($result-> num_rows > 0){
    while($row = $result-> fetch_assoc()){

        $theRowId = $row['ID'];

        if(isset($_POST['commentSubmit'])){
            $comment = $_POST['comments'];

            if(!empty($comment)){
                $sqlss = "INSERT INTO table_comment(parent_id, comment) VALUES ('$theRowId', '$comment')";
                $resultss = $conn->query($sqlss);
            }
        }

我的表单html

<form method='POST' action='index.php' id='singleAmountForm'>
   <textarea class='txtarea' id='".$row['ID']."' name='comments' placeholder='Comment...' rows=6></textarea>
   <button type='submit' name='commentSubmit' id='insertComment' class='visa-fler'>Add comment</button>
</form>

View example table_info = table_server

1 个答案:

答案 0 :(得分:0)

将行ID放在隐藏的输入中或提交按钮的值中。

afterEvaluate {
    generateBlueReleaseBuildConfig.enabled = false
    generateBlueDebugBuildConfig.enabled = false
    generateRedReleaseBuildConfig.enabled = false
    generateRedDebugBuildConfig.enabled = false
}

我还更改了表单中的所有ID,以便它们包含行ID,因为您在DOM中不应有重复的ID(但是这些元素甚至不需要ID,因为它们是动态更改的)。

然后,您可以插入注释(不需要在循环中完成)。最好使用准备好的语句来防止SQL注入。

<form method='POST' action='index.php' id='singleAmountForm".$row['ID']."'>
   <textarea class='txtarea' id='".$row['ID']."' name='comments' placeholder='Comment...' rows=6></textarea>
   <button type='submit' name='commentSubmit' id='insertComment".$row['ID']."' class='visa-fler' value='".$row['ID']"'>Add comment</button>
</form>
相关问题