2010-11-08 18 views
1

我已經創建了一個腳本,允許您刪除參加某個晚上的所有用戶。在選擇全部刪除它們之前,腳本將在表格中顯示所有這些用戶。出於某種原因,錯過了,雖然第一條記錄...爲什麼查詢錯過了第一行?

<?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

回答

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。由於看起來你對這個變量沒有做任何事情,我建議只刪除這一行。

+0

ah ha your right,thanks! – alsweet 2010-11-08 00:47:30

1

首先你得1排在

$row = mysqli_fetch_array ($r, MYSQLI_NUM); 

,然後你開始你的循環。

刪除第一行thingy,因爲它總是會跳過第0行這樣:-)

相關問題