2017-04-23 40 views
0

在準備聲明之前,當我通過ID搜索它正在工作,但添加準備聲明後結果是:ID不存在!添加準備陳述結果破碎的代碼

這是我的代碼:

<form action="" method="post"> 
    <div class="col-lg-3"> 
     <label for=""><h4>Search by ID</h4></label> 
     <div class="input-group"> 
      <input name="search" type="number" class="form-control"> 
      <span class="input-group-btn"> 
       <button name="submit" class="btn btn-default" type="submit"> 
      <span class="glyphicon glyphicon-search"></span> 
      </button> 
      </span> 
     </div> 
<hr> 
    </div> 
    <div class="col-lg-12"> 
     <div class="table-responsive"> 
      <table class="table table-hover"> 
       <thead> 
        <tr> 
         <th>ID</th> 
         <th>Ime</th> 
         <th>Prezime</th> 
        </tr> 
       </thead> 
       <tbody> 
       </tbody> 

       <?php 

       if(isset($_POST['search'])) { 

       $search_id = $_POST['search']; 

       mysqli_set_charset($connection, "utf8"); 
       $stmt = mysqli_prepare($connection, "SELECT id, ime, prezime FROM anketa WHERE id = ? LIMIT 1"); 

       if(isset($stmt)) { 

        mysqli_stmt_bind_param($stmt, 'i', $search_id); 

        mysqli_stmt_bind_result($stmt, $ank_id, $ank_ime, $ank_prezime); 

        mysqli_stmt_execute($stmt); 

       } 

       if(!$stmt) { 
        die("QueryFailed" . mysqli_error($connection)); 
       } 

       if(mysqli_stmt_num_rows($stmt) > 0) { 

        while(mysqli_stmt_fetch($stmt)): 

          echo "<tr>"; 
          echo "<td>{$ank_id}</td>"; 
          echo "<td>{$ank_ime}</td>"; 
          echo "<td>{$ank_prezime}</td>";  
          echo "<td><a class='btn btn-info btn-xs' href='search.php?source=edit&edit={$ank_id}'><i class='fa fa-pencil-square-o'></i> Edit</a></td>"; 
          echo "<td><a class='btn btn-danger btn-xs' onClick=\"javascript: return confirm('Delete?'); \" href='search.php?delete={$ank_id}'><i class='fa fa-trash-o'></i> Delete</a></td>"; 

        endwhile; 

        } else { 

         echo "ID doesn't exist!"; 

        } 

       } 

       ?> 

      </table> 
     </div> 
    </div> 

<?php 

if(isset($_GET['delete'])) { 

$ank_id = $_GET['delete']; 

if(isset($_SESSION['user_role'])) { 

if($_SESSION['user_role'] == 'admin' || 'user_role'] == 'superadmin') { 

$the_anketa_id = mysqli_real_escape_string($connection, $_GET['delete']); 

$query = "DELETE FROM anketa WHERE id = {$ank_id} "; 
$delete_anketa = mysqli_query($connection, $query); 
header("Location: search.php"); 

} 

} 

} 

?> 

</form> 
</div> 

<?php 
if(isset($_GET['source'])) { 
$source = $_GET['source']; 
if($source = 'edit') { 
include "includes/edit.php"; 
} elseif($source = 'submit') { 
header("Location: search.php"); 
} 
} 
?> 

<?php 
} else { 
echo "<script>alert('No access!')</script>"; 
} 
?> 

我不知道問題是刪除準備語句或代碼,因爲沒有準備發言了嗎?

+1

請閱讀[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。你有很多代碼是沒有必要的。 – Pharaoh

+0

對不起,我將來會這樣做的。 – shone83

+0

如果您編輯您的問題並刪除所有HTML內容(以及其他不重要的內容),那麼您可以針對此問題獲得更快更好的答案。 – Pharaoh

回答

0

這可能是因爲mysqli_stmt_num_rows($ stmt)總是返回零。

PHP文件: http://php.net/manual/en/mysqli-stmt.num-rows.php

您可能需要先調用mysqli_stmt_store_result()之前,你可以得到函數返回正確的值。

+0

它不起作用。如果我理解你,我做到了這一點: if(isset($ stmt)){mysqli_stmt_bind_param($ stmt,'i',$ search_id); mysqli_stmt_bind_result($ stmt,$ ank_id,$ ank_ime,$ ank_prezime); mysqli_stmt_execute($ stmt); mysqli_stmt_store_result($ stmt); } 我嘗試了許多與此功能的其他組合,但沒有結果... – shone83