开发者

Why is query missing out first row?

开发者 https://www.devze.com 2023-01-24 11:07 出处:网络
i\'ve created a script that allows you to delete all users who are attending a certain night. The script开发者_Python百科 will show all of these users in a table before you select to delete them all.

i've created a script that allows you to delete all users who are attending a certain night. The script开发者_Python百科 will show all of these users in a table before you select to delete them all. For some reason it misses out the first record though...

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


?>

im not sure where the error will lie so i've included all the code from the page, sorry.

any ideas?

thanks

alsweet


Because of this:

if (mysqli_num_rows($r) >0) { // valid id, show the form
   // get the user's information
   $row = mysqli_fetch_array ($r, MYSQLI_NUM);

You are doing this before you loop over all the records in your while loop. The last line will fetch the first record into the variable $row. As it seems that you don't do anything with this variable, I suggest to just delete this line.


first you get 1 row at

$row = mysqli_fetch_array ($r, MYSQLI_NUM);

and then you start your loop.

remove the first row thingy because it will always skip row 0 this way :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消