简体   繁体   English

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

[英]<button> execute mysql query on click

I am new to PHP and I am trying to execute a remove query when the remove button is clicked. 我是PHP新手,我试图在单击删除按钮时执行删除查询。 At the moment the current if statement is not triggered, so how can I trigger the if statement from a button and how can I get the id of the button that was tapped? 目前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']);
}

you can simply change the button to link and get the id using $_GET 您只需更改按钮即可链接并使用$_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']);
 }
<?
     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']);
    }
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM