2014-12-09 22 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