【发布时间】:2017-04-23 15:01:25
【问题描述】:
在我通过 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>";
}
?>
我不知道问题是准备语句还是删除代码,因为还没有准备语句?
【问题讨论】:
-
请阅读How to create a Minimal, Complete, and Verifiable example。你有很多不必要的代码。
-
对不起,我以后会这样做的。
-
如果您编辑您的问题并删除所有 HTML 内容(以及其他所有不重要的内容),您将获得更快更好的答案。
-
我尽可能多地编辑了,我希望现在没问题...我担心如果再编辑一些内容将不会显示问题...
-
那就测试吧!将您的代码减少到最低限度,看看错误是否仍然发生。如果不是,找出代码的哪一部分负责。一旦你可以将代码减少到最少,错误就更容易找到了。
标签: php mysql mysqli prepared-statement