2016-11-21 50 views
0

以下是代碼。它完美地顯示了我想要的所有元素。但是,當我點擊「註冊」時,它僅爲所選元素註冊一個元素。什麼是可能的問題?無法訪問數據庫中給出的所有元素

下面的代碼:因爲你沒有與提交enroll按鈕發送所需的課程ID爲腳本一起

if(array_key_exists("id", $_SESSION)) { 

    include("connection.php"); 
    $query = "SELECT * from `classes`";  

    echo '<div id="accordion" role="tablist" aria-multiselectable="true"> '; 

    $result = mysqli_query($link, $query); 
    $results = array(); 

    $rows = array(); 
    $queries = array(); 

    while($row = mysqli_fetch_array($result)) { 
     array_push($rows, $row); 
    } 

    for ($i = 0; $i < count($rows); $i ++) { 

     $name = $rows[$i]["name"]; 
     $start_time = $rows[$i]["begTime"]; 
     $end_time = $rows[$i]["endTime"]; 

     $price = $rows[$i]["price"]; 
     $places = $rows[$i]["placesAvailable"]; 

     if (array_key_exists("enroll", $_POST)) { 
      $queries[$i] = "UPDATE `users` SET sectionID = ".$rows[$i]["ID"]." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1"; 

      $results[$i] = mysqli_query($link, $queries[$i]);    
     } 

     echo ' 
      <div class="card"> 
       <div id = "section" class="card-header" role="tab" id="headingOne"> 
        <h5 class="mb-0"> 
         <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> 
          '.$name.' 
         </a> 
        </h5> 
       </div> 

       <div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne"> 
        <div class="card-block"> 
        '.$name.' 
         <p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p> 
         <p> <strong> Price: </strong>'.$price.' </p> 
         <p> <strong> Places available: </strong>'.$places.' </p> 
         <form action="" method = "post"> 
          <input class="btn btn-success" type="submit" name = "enroll" value = "Enroll"> 
         </form> 
        </div> 
       </div> 
      </div>    
     '; 
    } 
    echo '</div>'; 
} 
+0

您只更新更新查詢中的1個項目(用戶)。 'WHERE id =「.mysqli_real_escape_string($ link,$ _SESSION ['id'])'。我不確定我是否明白你想要做什麼,但在我看來,你需要在表單中發佈一些數據。你能否詳細解釋一下我想要達到的目標? –

+0

我希望用戶註冊課程,我有一個包含課程列表和一個按鈕的頁面,然後點擊下一步按鈕到特定的課程,我想更新數據庫,說當前用戶註冊了該課程。我怎樣才能修改我的代碼來實現這一目標? –

回答

0

你的邏輯是不完全正確的。 我已經調整了你的代碼。 如果您的用戶/學生當時只需要註冊一門課程,該邏輯將僅適用。

if(array_key_exists("id", $_SESSION)) { 

include("connection.php"); 

     if (array_key_exists("enroll", $_POST)) { 
      $query = "UPDATE `users` SET sectionID = ".mysqli_real_escape_string($link, $_POST["ID"])." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1"; 

      mysqli_query($link, $query); 
     } 


    $query = "SELECT * from `classes`"; 


    echo '<div id="accordion" role="tablist" aria-multiselectable="true"> '; 

    $result = mysqli_query($link, $query); 

    while($row = mysqli_fetch_array($result)) { 

     $name = $row["name"]; 

     $start_time = $rows["begTime"]; 

     $end_time = $rows["endTime"]; 

     $price = $rows["price"]; 

     $places = $rows["placesAvailable"]; 



     echo ' 

      <div class="card"> 
       <div id = "section" class="card-header" role="tab" id="headingOne"> 
        <h5 class="mb-0"> 
         <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> 
          '.$name.' 
         </a> 
        </h5> 
       </div> 

       <div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne"> 
        <div class="card-block"> 
        '.$name.' 
         <p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p> 
         <p> <strong> Price: </strong>'.$price.' </p> 
         <p> <strong> Places available: </strong>'.$places.' </p> 
         <form action="" method = "post"> 
          <input type="hidden" name="ID" value="'.$row["ID"].'" /> 
          <input class="btn btn-success" type="submit" name = "enroll" value = "Enroll"> 
         </form> 
        </div> 
       </div> 
      </div> 

     '; 
    } 

    echo '</div>'; 

} 

UPDATE 讓用戶選擇多個課程,你將只需要1形式複選框陣列。你的$_POST["ID"]將是一個類ID的數組。在下面的示例中,假設sectionID是字符串,它將被序列化存儲在數據庫中。在將數組保存到數據庫之前,您也可以使用json_encode。如果你想sectionID是數字,你需要創建一個新表來存儲每個用戶的所有類。

if(array_key_exists("id", $_SESSION)) { 

include("connection.php"); 

     if (array_key_exists("enroll", $_POST)) { 
      $query = "UPDATE `users` SET sectionID = ".mysqli_real_escape_string($link, serialize($_POST["ID"]))." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1"; 

      mysqli_query($link, $query); 
     } 


    $query = "SELECT * from `classes`"; 


    echo ' <form action="" method = "post"> 
      <div id="accordion" role="tablist" aria-multiselectable="true"> '; 

    $result = mysqli_query($link, $query); 

    while($row = mysqli_fetch_array($result)) { 

     $name = $row["name"]; 

     $start_time = $rows["begTime"]; 

     $end_time = $rows["endTime"]; 

     $price = $rows["price"]; 

     $places = $rows["placesAvailable"]; 



     echo ' 

      <div class="card"> 
       <div id = "section" class="card-header" role="tab" id="headingOne"> 
        <h5 class="mb-0"> 
         <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> 
          '.$name.' 
         </a> 
        </h5> 
       </div> 

       <div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne"> 
        <div class="card-block"> 
        '.$name.' 
         <p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p> 
         <p> <strong> Price: </strong>'.$price.' </p> 
         <p> <strong> Places available: </strong>'.$places.' </p> 

          <input type="checkbox" name="ID[]" value="'.$row["ID"].'" />Enroll 
        </div> 
       </div> 
      </div> 

     '; 
    } 

    echo '</div> 
    <input class="btn btn-success" type="submit" name = "enroll" value = "Submit"> 
    </form>'; 

} 
+0

非常感謝!請給出一個提示如何實現相同的事情,但讓用戶註冊幾門課程? –