2017-09-21 84 views
0

我想使用$ _GET ['student_id']顯示所有具有相同ID的行,但數據表錯誤,這是我的代碼。一旦我從url得到id例如: example.com/example.php?=2222

所有帶2222的id都會顯示出來。

Fetch.php

<?php 
include('db.php'); 
include('function.php'); 
$query = ''; 
$output = array(); 
$id = $_GET['student_id']; 
$query = 'SELECT * FROM personal WHERE student_id LIKE "%'.$_GET["student_id"].'%" '; 
$statement = $connection->prepare($query); 
$statement->execute(); 
$result = $statement->fetchAll(); 
$data = array(); 
$filtered_rows = $statement->rowCount(); 
foreach($result as $row) 
{ 
    $image = ''; 
    if($row["image"] != '') 
    { 
     $image = '<img src="upload/'.$row["image"].'" class="img-thumbnail" width="50" height="35" />'; 
    } 
    else 
    { 
     $image = ''; 
    } 
    $sub_array = array(); 
    $sub_array[] = $row["student_id"]; 
    $sub_array[] = $row["firstname"]." ".$row['middlename']." ".$row['lastname']; 
    $sub_array[] = '<a href="edit.php?student_id='.$row["student_id"].'" class="btn btn-info btn-xs update">Personal Info</a>'." ". 
    '<button type="button" id="'.$row["student_id"].'" class="btn btn-info btn-xs update">Grades</button>'." ". 
    '<button type="button" id="'.$row["student_id"].'" class="btn btn-info btn-xs update">Payment</button>'; 
    $sub_array[] = '<a href="view.php?student_id='.$row["student_id"].'" class="btn btn-primary btn-xs update">View</a>'; 
    $sub_array[] = '<button type="button" name="delete" id="'.$row["student_id"].'" class="btn btn-danger btn-xs delete">Delete</button>'; 
    $data[] = $sub_array; 
} 
$output = array(
    "draw"    => intval($_POST["draw"]), 
    "recordsTotal"  => $filtered_rows, 
    "recordsFiltered" => get_total_all_records(), 
    "data"    => $data 
); 
echo json_encode($output); 
?> 

數據表呼叫 我想顯示使用$ _GET相同的ID [ 'student_id數據'],但數據表的錯誤,這是我的代碼所有的行。一旦我得到的ID從URL例如: example.com/example.php?=2222

<script type="text/javascript" language="javascript" > 
    $(document).ready(function(){ 
     $('#add_button').click(function(){ 
      $('#user_form')[0].reset(); 
     }); 

     var dataTable = $('#user_data').DataTable({ 
      "processing":true, 
      "serverSide":true, 
      "order":[], 
      "ajax":{ 
       url:"fetch.php", 
       type:"POST" 
      }, 
      "columnDefs":[ 
       { 
        "targets":[0, 3, 4], 
        "orderable":false, 
       }, 
      ], 

     }); 


     $(document).on('click', '.delete', function(){ 
      var user_id = $(this).attr("id"); 
      if(confirm("Are you sure you want to delete this?")) 
      { 
       $.ajax({ 
        url:"delete.php", 
        method:"POST", 
        data:{user_id:user_id}, 
        success:function(data) 
        { 
         alert(data); 
         dataTable.ajax.reload(); 
        } 
       }); 
      } 
      else 
      { 
       return false; 
      } 
     }); 


    }); 
    </script> 

這是我所有的在我過去的問題與代碼。 我想使用$ _GET ['student_id']顯示所有具有相同ID的行,但數據表錯誤,這是我的代碼。一旦我得到的ID從URL例如: example.com/example.php?=2222

+0

你沒有'student_id'在您的網址。是錯字嗎?應該是'example.com/example.php?student_id = 2222' –

+0

對不起,它是一個錯字。但讓我們假設它的網址是正確的。但datatables總是顯示一個錯誤 – Aizen

+0

爲什麼不'SELECT * FROM personal WHERE student_id = $ id'你不應該使用像,你需要了解sql注入 – rtfm

回答

0

第一件事好像你正在使用PDO作爲數據基礎接入層和你prepare荷蘭國際集團的SQL但你直接在SQL之內直接使用變量犯了一個很大的錯誤。不要這樣做,如果你已經知道如何準備SQL,那麼它會打開SQL Injection的大門,然後使用bind將變量綁定到變量名稱。

另外我看到你綁定$_GET['student_id']$id但沒有以任何方式使用它,你真的不需要將接收到的變量綁定到新的變量。

要回答你的問題試試這個,

代碼:

$query = 'SELECT * FROM personal WHERE student_id LIKE "%'.$_GET["student_id"].'%" '; 
$statement = $connection->prepare($query); 
$statement->execute(); 
$result = $statement->fetchAll(); 

我的建議:

$query = 'SELECT * FROM personal WHERE student_id LIKE :id '; 
$statement = $connection->prepare($query); 
$statement -> bindParam(':id', $_GET['student_id']); 
$statement->execute(); 
$result = $statement->fetch(PDO::FETCH_ASSOC);