2016-06-09 79 views
0

我正在嘗試完成我的新個人CMS(內容管理系統),並且我已經編寫了一個部分,其中包括使用PHP,MySql和bootstrap Modal插入,選擇和刪除項目。 顯然我有更新查詢的麻煩。該過程是當我點擊編輯按鈕,模態Bootstrap將顯示,然後關聯數據將通過值屬性檢索表單Mysql。比我改變他們後點擊提交按鈕。問題在這裏!當過程完成時,我的$ _POST ['input_result']會出現錯誤。 var_dump獲得NULL,最後我有未定義的索引。 有沒有人找出這個問題?謝謝你們。 這裏是我的模式引導代碼PHP:使用模式Bootstrap更新查詢獲取未定義索引

<?php 
    $selectForUpdate = "SELECT * FROM ring WHERE id='4'"; 
    $resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate); 

    $first = $second = $third= ''; 
    $first = $_POST['u_ringCode']; 
    $second = $_POST['u_ringWeight']; 
    $third = $_POST['u_ringComment']; 

    $updateQuery = "UPDATE ring SET ring_code='".$first."', ring_weight='".$second."', ring_comment='".$third."' WHERE id='4'"; 
    $resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery); 
    while ($showUpdateRows = mysqli_fetch_assoc($resultSelectForUpdate)) { 
?> 
    <!-- Update Modal --> 
    <div class="modal fade bs-example-modal-sm" id="update-4" tabindex="-1" role="dialog" aria-labelledby="update-4-label" > 
     <div class="modal-dialog modal-sm" role="document"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h5 class="modal-title text-center" id="update-4-label" style="color:#ddd;" > 
         Edit Code 
        </h5> 
       </div> 
       <div class="modal-body"> 
        <div class="col-md-12 col-sm-12 col-xs-12"> 
         <form method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
          <div class="form-group"> 
           <label for="">Ring Code :</label> 
           <input type="text" class="form-control" id="" name="u_ringCode" placeholder="" value="<?=$showUpdateRows['ring_code']?>"> 
          </div> 
          <div class="form-group"> 
           <label for="">Ring Weight :</label> 
           <input type="text" class="form-control" id="" name="u_ringWeight" placeholder="" value="<?=$showUpdateRows['ring_weight']?>"> 
          </div> 
          <div class="form-group"> 
           <label for="">Comment :</label> 
           <input type="text" class="form-control" id="" name="u_ringComment" placeholder="" value="<?=$showUpdateRows['ring_comment']?>"> 
          </div> 
         </form> 
        </div> 
       </div> 
       <div class="modal-footer"> 
        <a type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button> 
        <a type="submit" class="btn btn-default pull-left" name="ringDelete" style="color:black;" href="?update=4">Submit</a> 
       </div> 
      </div> 
     </div> 
    </div> 
<?php 
    } 
?> 
+0

你爲什麼不使用提交按鈕 –

回答

0
<?php 
    /*Code Start For Connnect Database*/ 
     $connect_to_db=mysqli_connect("localhost", "root", "", "db_name"); 
    /*Code End For Connnect Database*/ 
    /*Code Start For Update Modal Form*/ 
     if (isset($_REQUEST['ringUpdate'])) { 
      $first = $_POST['u_ringCode']; 
      $second = $_POST['u_ringWeight']; 
      $third = $_POST['u_ringComment']; 
      $updateQuery = "UPDATE ring SET ring_code='".$first."', ring_weight='".$second."', ring_comment='".$third."' WHERE id='4'"; 
      $resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery); 
     } 
    /*Code End For Update Modal Form*/ 
     $selectForUpdate = "SELECT * FROM ring WHERE id='4'"; 
     $resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate); 
      while ($showUpdateRows = mysqli_fetch_assoc($resultSelectForUpdate)) { 

?> 
    <!-- Update Modal --> 
    <div class="modal fade bs-example-modal-sm" id="update-4" tabindex="-1" role="dialog" aria-labelledby="update-4-label" > 
     <div class="modal-dialog modal-sm" role="document"> 
      <div class="modal-content"> 
       <form method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
       <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <h5 class="modal-title text-center" id="update-4-label" style="color:#ddd;" > 
        Edit Code 
       </h5> 
      </div> 
      <div class="modal-body"> 
       <div class="col-md-12 col-sm-12 col-xs-12"> 
         <div class="form-group"> 
          <label for="">Ring Code :</label> 
          <input type="text" class="form-control" id="" name="u_ringCode" placeholder="" value="<?=$showUpdateRows['ring_code']?>"> 
         </div> 
         <div class="form-group"> 
          <label for="">Ring Weight :</label> 
          <input type="text" class="form-control" id="" name="u_ringWeight" placeholder="" value="<?=$showUpdateRows['ring_weight']?>"> 
         </div> 
         <div class="form-group"> 
          <label for="">Comment :</label> 
          <input type="text" class="form-control" id="" name="u_ringComment" placeholder="" value="<?=$showUpdateRows['ring_comment']?>"> 
         </div> 
       </div> 
      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button> 
       <input type="submit" class="btn btn-default pull-left" name="ringUpdate" style="color:black;" value="submit"> 
      </div> 
      </form> 
     </div> 
    </div> 
</div> 
    <?php 
} 
?> 
+0

它的工作夥伴!非常感謝你。我希望你能最好地認爲你想要的東西:D –

+0

Thanx,我很樂意幫助你 –

0

你的$ _ POST變量的形式提交前空。你需要檢查這個。另外,在不轉義字符串的情況下進行db查詢並不安全。您可以通過搜索MySQL注入來了解更多信息。第三件事是在更新查詢之前獲取項目。在這種情況下,您將看到頁面上的數據沒有變化。僅在頁面刷新後,它會顯示新值。你的代碼的 第一部分看起來是這樣的:

<?php 
if(!empty($_POST['u_ringCode'])&&!empty($_POST['u_ringWeight'])&&!empty($_POST['u_ringComment'])) 
{ 
    $first = $connect_to_db->escape_string($_POST['u_ringCode']); 
    $second = $connect_to_db->escape_string($_POST['u_ringWeight']); 
    $third = $connect_to_db->escape_string($_POST['u_ringComment']); 
    $updateQuery = "UPDATE ring SET ring_code='$first', ring_weight='$second', ring_comment='$third' WHERE id='4'"; 
    $resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery); 
} 
$selectForUpdate = "SELECT * FROM ring WHERE id='4'"; 
$resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate); 


while ($showUpdateRows = mysqli_fetch_assoc($resultSelectForUpdate)) { 
    ?>