2014-12-06 39 views
4

我是PHP新手,我試圖在單擊刪除按鈕時執行刪除查詢。目前if語句沒有被觸發,那麼我怎樣才能觸發按鈕中的if語句,以及如何獲取被點擊的按鈕的id?<button>點擊執行mysql查詢

<? 

    while($row = mysqli_fetch_array($result)) { 
     echo "<td>" . $row["id"] . "</td>"; 
     echo "<td>" . $row["teamName"] . "</td>"; 
     echo "<td>" . $row["name"] . "</td>"; 
     echo "<td>" . $row["country"] . "</td>"; 
     echo "<td>" . $row["birthday"] . "</td>"; 
     echo "<td><button type='button' class='btn btn-default ' aria-label='Left Align'>Edit</button> "; 
     echo "<button type='button' class='btn btn btn-danger' aria-label='Left Align' name='remove' value='remove'>Remove</button></td>"; 


    } 
?> 

if(isset($_POST['remove'])){ 
    $removeQuery = "UPDATE Players Where id='ID PRESSED?'"; 
    header('Location: '.$_SERVER['REQUEST_URI']); 
} 
+1

旋轉按鈕與$ _GET變量的鏈接獲得ID,** delete.php?ID = **然後抓住id的刪除並將其設置爲查詢中的id。 @Peter – xtrman 2014-12-06 20:14:23

回答

0

,你可以簡單地改變按鈕鏈接,並使用$_GET

<?php 

while($row = mysqli_fetch_array($result)) { 
    echo "<td>" . $row["id"] . "</td>"; 
    echo "<td>" . $row["teamName"] . "</td>"; 
    echo "<td>" . $row["name"] . "</td>"; 
    echo "<td>" . $row["country"] . "</td>"; 
    echo "<td>" . $row["birthday"] . "</td>"; 
    echo "<td><button type='button' class='btn btn-default ' aria-label='Left Align'>Edit</button> "; 
    // change yourpage.php to the page that executes the query 
    echo "<a href='yourpage.php?del=".$row['id']."' class='btn btn btn-danger' aria-label='Left Align' name='remove' value='remove'>Remove</button></td>";  
} 


if(isset($_GET['del'])){ 
    $id = (int)$_GET['del']; 
    $removeQuery = "UPDATE Players Where id = $id"; 
    header('Location: '.$_SERVER['REQUEST_URI']); 
} 
0
<? 
    while($row = mysqli_fetch_array($result)) { 
     echo "<td>" . $row["id"] . "</td>"; 
     echo "<td>" . $row["teamName"] . "</td>"; 
     echo "<td>" . $row["name"] . "</td>"; 
     echo "<td>" . $row["country"] . "</td>"; 
     echo "<td>" . $row["birthday"] . "</td>"; 
     echo "<td><button type='button' class='btn btn-default ' aria-label='Left Align'>Edit</button> "; 
     echo "<a href='yourpage.php?id=".$row['id']."' class='btn btn btn-danger'>Remove</a></td>"; 
    } 

if(isset($_POST['id'])){ 
    $id = $_POST['id']; 
    $removeQuery = "UPDATE Players SET col_name = value WHERE id=".$id." "; 
    $result = mysql_query($removeQuery); 
    if($result) { 
     header('Location: '.$_SERVER['REQUEST_URI']); 
    } 
} 

?>