2014-12-09 26 views
-4

您好編碼器< 3我是一名初學者,在php,mysql的東西,現在我卡在一個地方。我有一個包含批准和拒絕按鈕的表,我希望每當我點擊批准按鈕時,它都會將其狀態更改爲mysql表中的批准狀態。我怎麼能做到這一點....通過點擊鏈接如何使用php腳本更改html表中的mysql列表

這裏是我的MySQL表

MySql Table

這裏是UI

UI of the table

和編碼是這樣

<?php 
// Connect to the database 
$dbLink = new mysqli('127.0.0.1', 'root', '', 'hct_db'); 
if(mysqli_connect_errno()) { 
    die("MySQL connection failed: ". mysqli_connect_error()); 
} 

// Query for a list of all existing files 
$sql = 'SELECT `quote_id`, `name`, `mime`, `size`, `created`, `status` FROM `quote`'; 
$result = $dbLink->query($sql); 

// Check if it was successfull 
if($result) { 
    // Make sure there are some files in there 
    if($result->num_rows == 0) { 
    echo '<p>There are no files in the database</p>'; 
} 
else {?> 

         <div class="table-responsive"> 
          <table class="table table-striped table-bordered table-hover" id="dataTables-example"> 
           <thead> 
            <tr> 
             <th>Quote ID</th> 
             <th>File Name</th> 
             <th>File</th> 
             <th>Size</th> 
            <th>Created</th> 
             <th></th> 
             <th></th> 
             <th></th> 

            </tr> 

           </thead><tbody> 
           <?php 
    while($row = $result->fetch_assoc()) { 
    if ($row["status"]=="Not Approved") 
    { 
     echo "<tr>"; 
    echo "<td>" . $row['quote_id'] . "</td>"; 

    echo "<td>" . $row['name'] . "</td>"; 

    echo "<td>" . $row['mime'] . "</td>"; 
    echo "<td>" . $row['size'] . "</td>"; 
    echo "<td>" . $row['created'] . "</td>"; 
echo '<td><a href="get_file.php?quote_id=' . $row['quote_id'] .'">Download</a></td>'; 
echo "<td><input type='submit' name='submit' value='Approved'></td>"; 
echo "<td><input type='submit' name='submit' value='Reject'></td>"; 

echo "</tr>"; 
    }} 
    ?> 
</tbody> 
</table> 
</div>        
          </div> 
         <!-- /.table-responsive --> 

<?php  
$result->free(); 
} 
// Close the mysql connection 
$dbLink->close();} 
?>  
+0

你需要使用AJAX調用PHP腳本,更改數據庫。然後使用javascript將信息更新到客戶端,而無需刷新。 – Mattigins 2014-12-09 12:30:46

+0

謝謝@Mattigins,你能給我一些與此相關的鏈接嗎? – AakkiRock 2014-12-09 12:31:52

+0

http://www.w3schools.com/ajax/ – Mattigins 2014-12-09 12:32:18

回答

1

1)您需要使用JAX。
2)對於每一個按鈕,你可以使用的形式,如:

<form method="post" action="approved.php" target="_self"> 
     <td> 
      <input type='submit' name='submit' value='Approved'> 
      <input type='hidden' name='quoteID' value='<?php echo $row['quote_id']?>'> 
     </td> 
    </form> 

approved.php:

mysqli_connect 
    $approvedElement = $_POST['quoteID']; 
    $query = 'UPDATE ... WHERE `Quote ID` = \''.$quoteID.'\' '; 
    mysqli_query($query); 

所以阿賈克斯之前,我建議你瞭解GET和POST方法的基礎知識。 在這個例子中,你需要:

• form, to redirect the user to another page (approved.php, rejected.php) 
• $_POST, in the second page to retrieve the ID of the approved element, and use it in the next step 
• mysql_query, after you have correctly coded the query and successfully connected to the   DB 
+0

我已經完成了上面提到的更新,但是如何在那裏做ajax? – AakkiRock 2014-12-10 06:30:56