如何从此HTML中获取数据以在SQL中进行查询? [英] How can I get data from this HTML to query in SQL?

查看:194
本文介绍了如何从此HTML中获取数据以在SQL中进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用于发送数据的按钮

 < a href =#myModaldata-toggle = modalid =78data-target =#edit-modal> < button type =button> 
< i class =glyphicon glyphicon-zoom-in>< / i>
< /按钮>< / 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)或die(mysql_error());
$ row_Recordset7 = mysql_fetch_assoc($ Recordset7);

echo $ row_Recordset7 ['EQ_ID']; ?>

脚本

 <脚本> $('#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 =#myModaldata-toggle =modalid =78class ='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)或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

 <脚本> 
$('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>


This is the button used to send the data

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

This code shows the data

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

I want $er to query in below

<?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

<script>
        $('#edit-modal').on('show.bs.modal', function(e) {

            var $modal = $(this),
                Id = e.relatedTarget.id;

                    $modal.find('.edit-content').html(Id);


        })
    </script>

解决方案

You can do it by using AJAX. Learn these:

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

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

Prepared statements will save you from SQL Injection attacks. Practice to use it specially if you're doing a query that has an input from user.

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 procedural 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 Using OOP mysqli (prepared statement)

$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>

这篇关于如何从此HTML中获取数据以在SQL中进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆