添加prepare语句会导致代码损坏

时间:2017-04-23 15:01:25

标签: php mysql mysqli prepared-statement

在我按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 个答案:

答案 0 :(得分:0)

这可能是因为mysqli_stmt_num_rows($ stmt)总是返回零。

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

您可能需要首先调用mysqli_stmt_store_result(),然后才能使该函数返回正确的值。