如何通过PHP运行SQL更新查询?

如何通过PHP运行SQL更新查询?,php,mysql,button,onclick,Php,Mysql,Button,Onclick,过去两周来,这个问题一直困扰着我。我已经看完了书中几乎所有与我有关的问题,但没有一个答案对我有帮助。有人能帮我吗 这是我的密码: <?php mysql_connect("localhost" , "Brian" , "pass123") or die("can't connect"); mysql_select_db("2015") or die("Can't Select"); $result = mysql_query("SELECT Username, Password, Fi

过去两周来,这个问题一直困扰着我。我已经看完了书中几乎所有与我有关的问题,但没有一个答案对我有帮助。有人能帮我吗

这是我的密码:

<?php
mysql_connect("localhost" , "Brian" , "pass123") or die("can't  connect");
mysql_select_db("2015") or die("Can't Select");

$result = mysql_query("SELECT Username, Password, FirstName, LastName, Status FROM Users");  



echo "<hr><br><br><br><center><div class=table-responsive>
<table class='table-bordered table-hover' width=50%>
<tr>
    <td colspan=6 bgcolor=white><center><div><h1><b>Users</b></h1></div>    </center></td>
</tr>
<tr>
    <th><div><h2><b>&nbsp;Username&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;Password&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;First Name&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;Last Name&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;Current &nbsp;&nbsp;Status&nbsp;&nbsp;</h2></div>  </b></th>
    <th><div><h2><b>&nbsp;Change &nbsp;&nbsp;Status&nbsp;&nbsp;</h2></div></b></th>
</tr>";

while ($row = mysql_fetch_array($result)) 
{
   echo "<tr><td><b>&nbsp;&nbsp;<center>".$row['Username']."</b><br><br></td><td><b>&nbsp;&nbsp;<center>".$row['Password']."</center><br></b></td><td><b>&nbsp;&nbsp;<center>".$row['FirstName']."</center></b><br></td><td><b>&nbsp;&nbsp;<br><center>".$row['LastName']."</center><br><br></b></td><td><b>&nbsp;&nbsp;<center>".$row['Status']."d</center></b><br></td><td>";

if ($row['Status'] == "Enable"){
   echo "<div><a href=\"USERS.php?id=".$row['ID']."\"><button class='btn btn-lg btn-danger btn-block' type='Status'><b>Disable</b></button></a></div></tr>";

   if($id != NULL){
    mysql_query("UPDATE `Floral` SET `Status`=\"Disable\" WHERE `ID` = ".$ID."");
   }
}
else if($row['Status'] == "Disable"){
   echo "<div><a href=\"USERS.php?ID=".$row['ID']."\"><button class='btn btn-lg btn-success btn-block' type='Status'><b>Enable</b></button></a></div>    </tr>";
       if($id != NULL){
       $query="UPDATE Floral SET Status='Enabled' WHERE Status='Disabled'";
           $result=mysql_query($query);
       }
    }
        }
?>
基本上,我显示的是数据库中的一个表。当为当前为“启用”的用户单击“禁用”按钮时,应运行MySQL语句,将特定行的状态更新为“禁用”。为了“启用”某一行,情况也是如此


请帮忙!两周来我一直在努力完成这项工作,有些晚上我熬夜试图解决这个问题。我将感谢任何帮助。如果有任何编辑,我应该对我的问题,让我知道,我会编辑它。我只是想把这个修好

我假设您正在阅读表格中的内容。您似乎对PHP感到困惑。它在服务器上运行,而不是在客户端

您的显示文件应该是

echo "<hr><br><br><br><center><div class=table-responsive>
<table class='table-bordered table-hover' width=50%>
<tr>
    <td colspan=6 bgcolor=white><center><div><h1><b>Users</b></h1></div></center></td>
</tr>
<tr>
    <th><div><h2><b>&nbsp;Username&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;Password&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;First Name&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;Last Name&nbsp;&nbsp;</h2></div></b></th>
    <th><div><h2><b>&nbsp;Current &nbsp;&nbsp;Status&nbsp;&nbsp;</h2></div>  </b></th>
    <th><div><h2><b>&nbsp;Change &nbsp;&nbsp;Status&nbsp;&nbsp;</h2></div></b></th>
</tr>";

while ($row = mysql_fetch_array($result)) 
{
   $id = $row['ID'];
   echo "<tr><td><b>&nbsp;&nbsp;<center>".$row['Username'].
        "</b><br><br></td> <td><b>&nbsp;&nbsp;<center>".$row['Password'].
        "</center><br></b></td><td><b>&nbsp;&nbsp;<center>".
        $row['FirstName']."</center></b><br></td><td><b>&nbsp;&nbsp;<br><center>".
        $row['LastName']."</center><br><br></b></td><td><b>&nbsp;&nbsp;<center>".
        $row['Status']."d</center></b><br></td><td>";

if ($row['Status'] == "Enable"){
   echo "<div><a href=\"USERS.php?id=".$id."&status=Disable\"><button class='btn btn-lg btn-danger btn-block' type='Status'><b>Disable</b></button></a></div></tr>";
   }
else if($row['Status'] == "Disable"){
   echo "<div><a href=\"USERS.php?ID=".$id."&status=Enable\"><button class='btn btn-lg btn-success btn-block' type='Status'><b>Enable</b></button></a></div>    </tr>";
    }
}
?>
然后就是USERS.PHP文件,它只执行更新。我已经删除了上面的update语句,因为它们位于第二个文件中,该文件只进行了修改。更新完成后,您始终可以从那里运行显示文件。我还向href添加了第二个参数,以传递该行应该启用还是禁用。当按下按钮时


有更好的方法可以做到这一切。当你有了这个功能并且厌倦了闪烁时,请阅读有关AJAX的文章。

你是否尝试回显$row['Status']?另外,停止使用不推荐的mysql_*函数;改用PDO/MySQLi。你的PHP代码看起来也很混乱。确保您检查了查询结果。最后,建议仅使用小写文件名。您不希望在while循环中执行禁用操作1您从未设置$id,但请使用if$id!=如果您使用的是$ID而不是$ID->,则为NULL,在中为2。。。其中'ID`=.$ID',如果你不使用WHERE ID,则在你的else中为3,如果你在显示按钮后试图执行更新,'4,则即使更新成功,那么你的按钮也将不准确。谢谢!我将试用此代码并让您知道!我真的很感谢你的帮助!