我创建了一个脚本,可让您删除参加特定夜晚的所有用户。在您选择全部删除之前,该脚本将在表格中显示所有这些用户。出于某种原因,它错过了第一条记录......
<?php # delete_guest.php
// this page is for deleting a guest
// this page is accessed through view_users.php
$page_title = 'Delete all Guests';
include ('includes/header.html');
echo '<h1>Delete all Guests for a night</h1>';
if ( (isset($_GET['id']))) //From view_user.php
{
$id = $_GET['id'];
}
elseif ( (isset($_POST['id']))) //Form Submission
{
$id = $_POST['id'];
}
else { //No valid ID, Kill the script
echo '<p class="error">This page has been accessed in error.</p>';
include ('includes/footer.html');
exit();
}
require_once ('../mysqli_connect.php');
// check if the form is submitted
if (isset($_POST['submitted'])) {
if ($_POST['sure'] == 'Yes') { //delete the records
$q = "DELETE FROM guests WHERE night_attending=$id";
$r = @mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) >0){ //if it ran ok
echo '<p>The guest has been deleted.</p';
}
else { // if it didn't run ok
echo '<p class="error">The guest could not be deleted due to a system error</p>';
echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // debugging message
}
}
else{ // no comfirmation of deletion
echo '<p>The guest has NOT been deleted.</p>';
}
}
else{ // show the form
$q = "SELECT last_name, first_name, user_id, email, DATE_FORMAT(night_attending, '%d/%m/%Y') AS na FROM guests WHERE night_attending='$id'"; // select the data
$r = @mysqli_query ($dbc, $q); // Run the query.
if (mysqli_num_rows($r) >0) { // valid id, show the form
// get the user's information
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
echo //create the form
'<form action="delete_allguests.php" method="post">
<p>
Are you sure you want to delete all the guests from this night?<br />
<input type="radio" name="sure" value="Yes" /> Yes
<input type="radio" name="sure" value="No" checked="checked"/> No
</p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
<hr />
</form>';
// Table header
echo '<br />
<table align="center" cellscpacing="3" cellpadding="3" width="75%">
<tr>
<td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td>
<td align="left"><b>Email</b></td></td>
<td align="left"><b>Date Attending</b></td>
</tr>';
// Fetch and print all the records:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '
<tr>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['email'] .'</td>
<td align="left">' . $row['na'] . '</td></tr>
';
}
echo '</table>'; // Close the table.
mysqli_free_result ($r); // Free up the resources.
} else { // If no records were returned.
echo '<p class="error">There are currently no guests on the list for this night.</p>';
}
}
mysqli_close($dbc);
include ('includes/footer.html');
?>
我不确定错误会出在哪里,所以我已经包含了页面中的所有代码,抱歉。
有什么想法吗?
谢谢
好甜
最佳答案
因为这个:
if (mysqli_num_rows($r) >0) { // valid id, show the form
// get the user's information
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
您在 循环 while
循环中的所有记录之前执行此操作。最后一行会将第一条记录提取到变量 $row
中。由于您似乎没有对这个变量做任何事情,我建议只删除这一行。
关于php - 为什么查询遗漏了第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4120513/