【问题标题】:MySQL data not outputting onto bootstrap modalMySQL数据未输出到引导模式
【发布时间】:2020-03-10 14:15:55
【问题描述】:

我正在尝试将数据输出到我的模式,但没有出现。我设法在我创建的表上输出了一些数据。但是,当尝试通过模态显示更多数据时,似乎什么也没有出现。

我一直在通过 PHP 寻找可能的修复,但没有运气。

下面的 php 旨在从我的news.php 文件接收到信息后将数据输出到模态:

<?php  
if(isset($_POST["employee_id"]))  
{  
  $output = '';  
  $connect = mysqli_connect("localhost", "root", "test1234", "news_db");  
  $query = "SELECT * FROM latest_news WHERE news_id = '".$_POST["employee_id"]."'";  
  $result = mysqli_query($connect, $query);  
  $output .= '  
  <div class="table-responsive">  
      <table class="table table-bordered">';  
  while($row = mysqli_fetch_array($result))  
  { 
    $output .= '  
     <tr>  
      <td width="30%"><label>Name</label></td>  
       <td width="70%">'.$row["news_title"].'</td>  
      </tr> 
      <tr>  
        <td width="30%"><label>Address</label></td>  
        <td width="70%">'.$row["news_text"].'</td>  
        </tr>  
    ';  
  } 
  $output .= "</table></div>";  
  echo $output;  
}  
?>

我的 news.php 文件中的这个表成功地从我的表中输出了所需的数据:

<div class="table-responsive">  
  <table class="table table-bordered">  
    <tr>  
      <th width="70%">Employee Name</th>  
      <th width="30%">View</th>  
    </tr>  
    <?php  
    while($row = mysqli_fetch_array($result)){  
    ?>  
      <tr>  
        <td><?php echo $row["news_title"]; ?></td>  
        <td><input type="button" name="view" value="view" id="<?php echo $row["news_id"]; ?>" class="btn btn-info btn-xs view_data" /></td>  
      </tr>  
    <?php  
    }  
    ?>  
  </table>  
</div>  

<div id="dataModal" class="modal fade">  
  <div class="modal-dialog">  
    <div class="modal-content">  
      <div class="modal-header">  
        <button type="button" class="close" data-dismiss="modal">&times;</button>  
        <h4 class="modal-title">Employee Details</h4>  
      </div>  
      <div class="modal-body" id="employee_detail"></div>  
      <div class="modal-footer">  
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
       </div>  
     </div>  
   </div>  
 </div>  

模态脚本:

 <script>  
 $(document).ready(function(){  
   $('.view_data').click(function(){  
     var employee_id = $(this).attr("news_id");  
     $.ajax({  
       url:"select.php",  
       method:"post",  
       data:{employee_id:employee_id},  
       success:function(data){  
         $('#employee_detail').html(data);  
         $('#dataModal').modal("show");  
       }  
     });  
   });  
 });
 </script> 

【问题讨论】:

  • 不显示数据是您最不必担心的,您应该首先研究如何防止 SQl 注入..
  • 我已经在缓解的事情,因为不需要用户输入而变得更简单@RaymondNijland
  • 您的代码易受 SQL 注入攻击。您应该使用准备好的语句。

标签: javascript php mysql phpmyadmin


【解决方案1】:

您的代码看起来不错,但您确定您使用的 SQL 查询是正确的吗?

$query = "SELECT * FROM latest_news WHERE news_id = '".$_POST["employee_id"]."'";

您将employee_id 用作news_id。这可能是问题所在。尝试获取 $query 值并在您的数据库上运行它。


顺便说一下,考虑在您的 PHP 脚本上使用 PDO 和准备好的语句。 filter_input() 还可以帮助您的代码更安全

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 1970-01-01
    • 2023-03-21
    • 2018-09-26
    • 2013-03-02
    • 2011-09-04
    • 2021-11-28
    相关资源
    最近更新 更多