小编典典

如何从HTML获取数据以在SQL中进行查询?

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>

阅读 204

收藏
2021-04-22

共1个答案

小编典典

您可以使用AJAX来完成。了解这些:

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

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

准备好的语句将使您免受SQL Injection攻击。如果要查询的是来自用户的输入,请练习专门使用它。

的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)使用程序化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>
2021-04-22