如何提交表格,并在每个不同的';选择mysql-query';并将答案插入mysql数据库


How to submit forms with answers in each different result of a 'select mysql query' and insert the answers in mysql database

我试过这个代码:

<?php   
$sql="SELECT id, comment FROM comments 
ORDER BY comments.id DESC";
$result = mysql_query($sql) or trigger_error ( mysql_error ( )); 
while($row = mysql_fetch_array($result))
{
  echo "<div>";
  echo $row['comment'];
  echo "</br>";
  // Answer to comment
    echo "<a id='href".$row['id']."' href='#'> Answer </a>";

    echo "<div>";
    echo "Your answer is: ";
    echo "<span id='answerdone'"; 
    echo "</span>";

  echo "</div>";
}
?>
<script>
       $('#href".$row['id']."').click(function() {
    $(this).empty().next.append(<div> <input type='text' value='' name='answer'   
                                 id='".$row['id']."'
                                 <input type='submit' value='Answer' />
                                 </div>);
        }); 

var input = document.getElementById('".$row['id']."'),
    placeholder = document.getElementById('answerdone');
input.onsubmit = function() {
   placeholder.innerHTML = input.value
} 
</script>

为了在MySQL查询的每个结果中都显示一个表单,以及在"您的答案是:"之后,在同一页上写下该表单中所写的内容。不幸的是,这不起作用,我不知道为什么。此外,我希望在"你的答案是:"之后写的内容作为附加列插入注释表中。

我该怎么做?出了什么问题?

您正在混合php和javascript变量。

例如:

echo "<a id='href".$row['id']."' href='#'> Answer </a>";

将显示为:

<a id='href1' href='#'> Answer </a>";

因此,您无法使用javascript:找到此元素

$('#href".$row['id']."').click(function() {

它必须是类似的东西

$('#href1').click(function() {

您可能应该看看您的javascript控制台。在那里你可以得到正确的错误描述。