<button>单击</button>执行mysql查询

时间:2014-12-06 20:10:01

标签: php mysql

我是PHP的新手,我在点击删除按钮时尝试执行删除查询。目前if语句未被触发,那么如何从按钮触发if语句,如何获取被点击的按钮的id?

<?

     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']);
}

2 个答案:

答案 0 :(得分:0)

您只需更改按钮即可链接并使用$_GET

获取ID
 <?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']);
 }

答案 1 :(得分: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']);
    }
}

?>