如何从此 HTML 获取数据以在 SQL 中查询


How can I get data from this HTML to query in SQL?

这是用于发送数据的按钮

  <a href="#myModal" data-toggle="modal" id="78" data-target="#edit-modal"> <button type="button"   >
    <i class="glyphicon glyphicon-zoom-in"></i>
    </button></a> 

此代码显示数据

 <?php  echo  $er="<div class='"modal-body edit-content'"></div>";  ?>

我想$er下面查询

<?php       
$str = "SELECT * FROM examquestion WHERE EQ_ID = '$er' ";
$Recordset7 = mysql_query($str) or die(mysql_error());
$row_Recordset7 = mysql_fetch_assoc($Recordset7);
echo $row_Recordset7['EQ_ID']; ?>

脚本

<script>
        $('#edit-modal').on('show.bs.modal', function(e) {
            var $modal = $(this),
                Id = e.relatedTarget.id;
                    $modal.find('.edit-content').html(Id);

        })
    </script>

您可以使用 AJAX 执行此操作。了解这些:

http://www.w3schools.com/ajax/

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

预准备语句将使您免受 SQL 注入攻击。练习在执行具有用户输入的查询时专门使用它。

.HTML

<a href="#myModal" data-toggle="modal" id="78" class='get-content' data-target="#edit-modal">
  <button type="button">
    <i class="glyphicon glyphicon-zoom-in"></i>
  </button>
</a>
<div id="edit-modal">
  <div class="modal-body edit-content"></div>
</div>

PHP (content.php) Using procedure mysql

<?php       
   $er = $_POST['id'];
   $str = "SELECT * FROM examquestion WHERE EQ_ID = '$er' ";
   $Recordset7 = mysql_query($str) or die(mysql_error());
   $row_Recordset7 = mysql_fetch_assoc($Recordset7);
   echo $row_Recordset7['EQ_ID'];  
?>

PHP 使用 OOP mysqli (预准备语句)

$mysqli = new mysqli("localhost", "user", "password", "database");
$query = "SELECT content FROM examquestion WHERE EQ_ID = ?";
if ( $stmt = $mysqli->prepare($query) )
{
    $stmt->bind_param("s", $er);
    if ( $stmt->execute() )
    {
       $stmt->bind_result($content);
       while ($stmt->fetch()) {
          echo $content;
       }
    }
    $stmt->close();
}

.JS

<script>
        $('a.get-content').click(function() {
            var contentId = $(this).attr('id');
            var modalId = $(this).attr('data-target');
            $.ajax({
                url: "content.php",
                type: "POST",
                data: {id: contentId},
                dataType: "html",
                success:function(data){
                    $(modalId).find('.edit-content').eq(0).html(data);
                 }
            });
        });
</script>