为什么查询缺少第一行?

时间:2010-11-08 00:38:30

标签: php mysql database

我创建了一个脚本,允许您删除所有参加某个晚上的用户。在您选择全部删除它们之前,该脚本将在表中显示所有这些用户。出于某种原因,它错过了第一张唱片但是......

<?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');


?>

我不确定错误的位置,所以我已经包含了页面中的所有代码,抱歉。

任何想法?

感谢

alsweet

2 个答案:

答案 0 :(得分:5)

因此:

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中。由于您似乎没有对此变量执行任何操作,我建议您删除此行。

答案 1 :(得分:1)

首先在

获得1行
$row = mysqli_fetch_array ($r, MYSQLI_NUM);

然后你开始循环。

删除第一行,因为它总是以这种方式跳过第0行: - )