2015-06-22 61 views
0

這個問題一直困擾着我過去兩週。我已經瀏覽了StackOverflow中與我有關的幾乎所有問題,但沒有任何答案對我有所幫助。有人可以幫幫我嗎?如何通過PHP運行SQL更新查詢?

這裏是我的代碼:

<?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語句,將該特定行的狀態更新爲'禁用'。同樣,爲了'啓用'特定行也是如此。

請幫助!我一直在努力完成這個爲期兩個星期,有些晚上我很晚才試着解決這個問題,我會很感激的,如果有任何編輯,我應該關於我的問題,讓我知道,我會編輯它,我只是希望得到這個固定的

+1

你試過'echo $ row ['Status']'?另外,停止使用不推薦使用的'mysql_ *'函數;改用PDO/MySQLi。你的PHP代碼也很麻煩。確保你檢查了查詢的結果。最後,建議僅使用小寫文件名。 – Raptor

+1

你不想在while循環中使用禁用動作 – 2015-06-22 03:49:06

+0

(1)你從不設置'$ id',但是用if($ id!= NULL)'檢查它,(2)在你的'if'中檢查使用'$ ID'而不是'$ id' - >'... WHERE \'ID \'=「。$ ID。」',和(3)在你的'else if'中你不使用' WHERE'ID',(4)你在**顯示按鈕後試圖執行'UPDATE' **,所以即使'UPDATE'成功了,你的按鈕也不準確。 – Sean

回答

0

我假設你正在從表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

+0

謝謝Rohit!我會試試這個代碼並讓你知道!我非常感謝幫助! – mkeerthi10