AJAX实时插入删除搜索,显示项目2次

时间:2016-06-23 03:51:57

标签: php mysql ajax

当我插入一个项目时,它会在列表中显示2次,但在mysql表上它只注册了一次。它开始于我开始使用会话时。提前谢谢。

的index.php

<html>  
  <head>  
       <title>Live Table Data Edit</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">Lista articulos prestados</h3><br />  
                 <div id="live_data"></div>                 
            </div>  
       </div>  
  </body>  
 </html>  
<script>  
 $(document).ready(function(){  
  function fetch_data()  
  {  
       $.ajax({  
            url:"select.php",  
            method:"POST",  
            success:function(data){  
                 $('#live_data').html(data);  
            }  
       });  
  }  
  fetch_data();  
  $(document).on('click', '#btn_add', function(){  
       var Articulo = $('#Articulo').text();  
       var fecha = $('#fecha').text(); 
       var emailpresta = $('#emailpresta').text();
       var emailrecibe = $('#emailrecibe').text(); 
       if(Articulo == '')  
       {  
            alert("Ingresa Articulo");  
            return false;  
       }  
       if(fecha == '')  
       {  
            alert("Ingresa Fecha");  
            return false;  
       }  
       if(emailpresta == '')  
       {  
            alert("Ingresa tu email");  
            return false;  
       }  
       if(emailrecibe == '')  
       {  
            alert("Ingresa email tercero");  
            return false;  
       }  
       $.ajax({  
            url:"insert.php",  
            method:"POST",  
            data:{Articulo:Articulo, fecha:fecha, emailpresta:emailpresta,     emailrecibe:emailrecibe },  
            dataType:"text",  
            success:function(data)  
            {  
                 alert(data);  
                 fetch_data();  
            }  
       })  
  });     
  $(document).on('click', '.btn_delete', function(){  
       var id=$(this).data("id3");  
       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  
session_start();

 $check_usuario = $_SESSION['email'];
 $connect = mysqli_connect("######", "######", "", "############");  
 $output = '';  
 $sql = "SELECT * FROM articulos INNER JOIN usuarios WHERE     emailpresta='".$check_usuario."'";  
 $result = mysqli_query($connect, $sql);  
 $output .= '  
  <div class="table-responsive">  
       <table class="table table-bordered">  
            <tr>  
                 <th width="10%">Id</th>  
                 <th width="40%">Articulo</th>  
                 <th width="40%">Fecha(Formato: AAAA-MM-DD)</th>  
                 <th width="40%">Email-presta</th>
                 <th width="40%">Email-recibe</th>    
                 <th width="10%">Borrar</th>  
            </tr>';  
 if(mysqli_num_rows($result) > 0)  
 {  
  while($row = mysqli_fetch_array($result))  
  {  
       $output .= '  
            <tr>  
                 <td>'.$row["id"].'</td>  
                 <td class="Articulo" data-id1="'.$row["id"].'"   contenteditable>'.$row["Articulo"].'</td>  
                 <td class="fecha" data-id2="'.$row["id"].'" contenteditable>'.$row["fecha"].'</td>  
                 <td class="emailpresta" data-id2="'.$row["id"].'" contenteditable>'.$row["emailpresta"].'</td> 
                 <td class="emailrecibe" data-id2="'.$row["id"].'" contenteditable>'.$row["emailrecibe"].'</td> 
                 <td><button type="button" name="delete_btn" data-   id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>  
            </tr>  
       ';  
  }  
  $output .= '  
       <tr>  
            <td></td>  
            <td id="Articulo" contenteditable></td>  
            <td id="fecha" contenteditable></td>  
            <td id="emailpresta" contenteditable></td> 
            <td id="emailrecibe" 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></td>
                      <td id="Articulo" contenteditable></td>  
                      <td id="fecha" contenteditable></td>  
                      <td id="emailpresta" contenteditable></td> 
                      <td id="emailrecibe" contenteditable></td> 
                      <td><button type="button" name="btn_add" id="btn_add"     class="btn btn-xs btn-success">+</button></td>  
                 </tr>';  
 }  
 $output .= '</table>  
  </div>';  
 echo $output;  
 ?>  

insert.php

<?php  
  session_start();
  $connect = mysqli_connect("############", "############", "", "############");  
 $sql = "INSERT INTO articulos(Articulo, fecha, emailpresta, emailrecibe)     VALUES('".$_POST["Articulo"]."', '".$_POST["fecha"]."'
    , '".$_POST["emailpresta"]."', '".$_POST["emailrecibe"]."')";  
 if(mysqli_query($connect, $sql))  
 {  
  echo 'Data Inserted';  
 }  
 ?>  

delete.php

  <?php  
  session_start();
  $connect = mysqli_connect("############", "############", "", "############");  
  $sql = "DELETE FROM articulos WHERE id = '".$_POST["id"]."'";  
  if(mysqli_query($connect, $sql))  
  {  
   echo 'Data Deleted';  
  }  
  ?>  

1 个答案:

答案 0 :(得分:0)

问题可能出在MySQL查询中。执行以下操作时屏幕上显示的内容:

//session_start();  <-- do not start session
$email = '......'; <-- use an email from the DB with data available
$sql = "SELECT * FROM articulos INNER JOIN usuarios 
                  WHERE emailpresta='$email'";
$connect = mysqli_connect(...);
$result = mysqli_query($connect,$sql);

$data = mysqli_fetch_all($result));
print_r($data);

你看到重复的行吗?如果是这样,您将需要处理SQL查询。具体来说,您不会告诉数据库如何加入articulosusarios表。而不是

SELECT * from articulos INNER JOIN usarios WHERE emailpresta='$email'

您可能需要以下内容:

SELECT * from articulos INNER JOIN usarios ON articulos.userID=usarios.ID
                                   WHERE usarios.emailpresta='$email'

使用匹配的两个表中的字段,而不是articulos.userIDusarios.ID。您可以了解一下INNER JOIN here

旁注

我注意到您将$_SESSION['email']直接插入数据库查询中。如果这是您从用户收到的值,则会产生SQL injection attack的巨大风险,这是任何人使用特制输入执行任何查询(包括提取,修改或删除数据库记录)的一种方式。请转而使用parameterized queries。即使$_SESSION['email']的内容是安全的,在您从用户那里获取输入的任何地方运行原始查询都是一种冒险的习惯。