【问题标题】:How to run SQL Update query through PHP?如何通过 PHP 运行 SQL 更新查询?
【发布时间】:2015-06-22 03:43:53
【问题描述】:

在过去的两周里,这个问题一直困扰着我。我浏览了 StackOverflow 中与我相关的几乎所有问题,但没有一个答案对我有帮助。有人可以帮帮我吗?

这是我的代码:

<?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 语句,将该特定行的状态更新为“禁用”。为了“启用”特定行也是如此。

请帮忙!为了解决这个问题,我已经尝试了两个星期,有些晚上我熬夜很晚才试图解决这个问题。我会感谢任何帮助。如果我应该对我的问题进行任何编辑,请告诉我,我将对其进行编辑。我只是想解决这个问题。

【问题讨论】:

  • 你试过echo $row['Status']吗?另外,停止使用已弃用的mysql_* 函数;改用 PDO / MySQLi。你的 PHP 代码看起来也很乱。确保您检查了查询结果。最后,建议只使用小写文件名。
  • 您不希望在 while 循环中使用禁用操作
  • (1) 你从未设置过$id,但用if($id != NULL) 进行检查,(2) 在你的if 中你使用的是$ID 而不是$id -> @987654329 @,以及 (3) 在您的 else if 中,您不使用 WHERE ID, and (4) you are trying to do the UPDATE` 您显示按钮之后,因此即使 UPDATE 成功,那么您的按钮会不准确。

标签: php mysql button onclick


【解决方案1】:

我假设您正在从表 FLORAL 中读取数据。您似乎对 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 文件。我已经从上面删除了更新语句,因为它们进入了仅进行修改的第二个文件。更新完成后,您始终可以从那里运行显示文件。我还向 href 添加了第二个参数,以传递是否应该启用或禁用该行。按下按钮时。

注意

有更好的方法来做这一切。当您完成这项工作并厌倦了闪烁时,请阅读有关 AJAX 的信息。

【讨论】:

  • 谢谢罗希特!我会试试这个代码,让你知道!非常感谢您的帮助!
猜你喜欢
  • 2023-01-11
  • 2021-11-12
  • 1970-01-01
  • 2012-11-01
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多