mysqli php文件没有识别来自另一个php文件的变量

时间:2017-12-17 10:26:44

标签: javascript php jquery ajax

我试图在数据库中显示表格上的数据。为此,我创建了一个date.php文件来选择日期并将该值发送到另一个select.php文件,该文件将允许我仅选择数据库中具有相同日期的数据。但问题是文件select.php无法识别此值。我甚至尝试过SELECT * FROM archireunion ORDER BY datereunion DESC WHERE datereunion =' 2017-10-23'它存在于数据库中,但它不起作用。 请帮帮我

date.php 这段代码允许我获取日期,然后将其发送到index.php文件,但select.php文件不识别变量

<html>
<body>

<form action="index.php" method="post">
  <input type=date value="madate">
  <input type="submit" value="Envoyer">
</form>

index.php所有函数都在这个文件中

<html>
      <head>
           <title>Liste des réunions</title>
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
      </head>
      <body>
           <div class="container">
                <br />
                <br />
                <br />
                <div class="table-responsive">
                     <h3 align="center">Liste des réunions</h3><br />
                     <div id="live_data"></div>
                </div>
           </div>
      </body>
 </html>
 <script>
 $(document).ready(function(){
      function fetch_data()
      {
          //var date1 = $_GET['madate']
           $.ajax({
                url:"select.php",
                method:"POST",
                //data:{date:date},
                success:function(data){
                     $('#live_data').html(data);
                }
           });
      }
      fetch_data();
      $(document).on('click', '#btn_add', function(){
           var titre = $('#titre').text();
           var datereunion = $('#datereunion').text();
           var description = $('#description').text();

           if(titre == '')
           {
                alert("Entrer le titre de la réunion");
                return false;
           }
           if(datereunion == '')
           {
                alert("Entrer la date de la réunion");
                return false;
           }
           if(description == '')
           {
                alert("Entrer la description de la réunion ");
                return false;
           }
           $.ajax({
                url:"insert.php",
                method:"POST",
                data:{titre:titre, datereunion:datereunion, description:description},
                dataType:"text",
                success:function(data)
                {
                     alert(data);
                     fetch_data();
                }
           })
      });
      function edit_data(id, text, column_name)
      {
           $.ajax({
                url:"edit.php",
                method:"POST",
                data:{id:id, text:text, column_name:column_name},
                dataType:"text",
                success:function(data){
                     alert(data);

                }
           });
      }
      $(document).on('blur', '.titre', function(){
           var id = $(this).data("id1");
           var titre = $(this).text();
           edit_data(id, titre, "titre");
      });
      $(document).on('blur', '.datereunion', function(){
           var id = $(this).data("id2");
           var datereunion = $(this).text();
           edit_data(id,datereunion, "datereunion");
      });
      $(document).on('blur', '.description', function(){
           var id = $(this).data("id3");
           var description = $(this).text();
           edit_data(id,description, "description");
      });
      $(document).on('click', '.btn_delete', function(){
           var id=$(this).data("id4");
           if(confirm("Are you sure you want to delete this?"))
           {
                $.ajax({
                     url:"delete.php",
                     method:"POST",
                     data:{id:id},
                     dataType:"text",
                     success:function(data){
                          alert(data);
                          fetch_data();
                     }
                });
           }
      });
 });
 </script>

select.php 该文件允许我在数据库中显示数据表

<?php
 $connect = mysqli_connect("localhost", "root", "", "architecture");
 $output = '';
 $sql = "SELECT * FROM archireunion ORDER BY datereunion DESC WHERE datereunion='".$_POST['madate']"'";
 $result = mysqli_query($connect, $sql);
 $output .= '
      <div class="table-responsive">
           <table class="table table-bordered">
                <tr>
                     <th width="10%">Id</th>
                     <th width="40%">Titre</th>
                     <th width="40%">Date</th>
                     <th width="40%">Description</th>
                     <th width="10%">Delete</th>

                </tr>';
 if(mysqli_num_rows($result) > 0)
 {
      while($row = mysqli_fetch_array($result))
      {
           $output .= '
                <tr>
                     <td>'.$row["id"].'</td>
                     <td class="titre" data-id1="'.$row["id"].'" contenteditable>'.$row["titre"].'</td>
                     <td class="datereunion" data-id2="'.$row["id"].'" contenteditable>'.$row["datereunion"].'</td>
                     <td class="description" data-id3="'.$row["id"].'" contenteditable>'.$row["description"].'</td>

                     <td><button type="button" name="delete_btn" data-id4="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
                </tr>
           ';
      }
      $output .= '
           <tr>
                <td></td>
                <td id="titre" contenteditable></td>
                <td id="datereunion" contenteditable></td>
                <td id="description" contenteditable></td>

                <td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
           </tr>
      ';
 }
 else
 {
      $output .= '<tr>
                          <td colspan="4">Pas de réunion</td>
                     </tr>';[enter image description here][1]
 }
 $output .= '</table>
      </div>';
 echo $output;

 ?>

1 个答案:

答案 0 :(得分:0)

select.php脚本没有获取madate变量。您是从index.php JS函数fetch_data调用它,但是没有在该调用上设置参数。

由于从fetch_data函数调用select.php,你必须在该函数上设置madate的值:

function fetch_data()
{
    $.ajax({
            url:"select.php",
            method:"POST",
            data:{madate: "<?=$_REQUEST['madate']?>"},
            success:function(data){
                 $('#live_data').html(data);
            }
       });
  }

此外,在date.php文件中,您没有设置变量的名称,只是设置值,它应该如下所示:

<html>
<body>

<form action="index.php" method="post">
  <input type="date" name="madate" />
  <input type="submit" value="Envoyer">
</form>

然后过程就像这样:

  • date.php允许用户选择日期。日期作为madate参数发送到index.php。

  • 在index.php中,您在fetch_data函数中注入madate的值,以便将其发送到select.php脚本。

  • select.php脚本接收madate变量并使用它来查询数据库。

顺便说一下,你的SQL代码是开放的邪恶注入,你必须总是使用用户发送的数据(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)查询数据库时使用预准备语句,或者清理任何参数您在查询中使用mysqli_real_escape_string(http://php.net/manual/en/mysqli.real-escape-string.php):

$date = mysqli_real_escape_string($connect, $_POST['madate']);
$sql = "SELECT * FROM archireunion ORDER BY datereunion DESC WHERE datereunion='$date'";
$result = mysqli_query($connect, $sql);