简体   繁体   中英

Fetching Table from MySQL using PHP and using a Button to Update?

I'm trying to create an admin system wherein the list of accounts will be displayed and under a column called "account status", I like to place a button which shows "ACTIVATE" if the fetched account status is Inactive (I have done it using Boolean wherein 1 is active and 0 is inactive).

However, I can't make the displayed button update the specific row wherein it is placed and I tried using an Update query which doesn't work. Is my idea possible or I have to resort to manual update using inputting of textfields? Hope you can help me as I'm new to PHP.

Here's my code:

    <?php
//FETCH DATA
$query=mysql_query("SELECT * FROM accounts");
    while($fetch=mysql_fetch_array($query)){

                $accountupdate = $fetch["accountid"];

                echo" <tr>
                <td><font face='verdana,arial' size=-1>".$fetch["accountid"]."</td>
                <td><font face='verdana,arial' size=-1>".$fetch["username"]."</td>
                <td><font face='verdana,arial' size=-5>".$fetch["password"]."</td>
                <td><font face='verdana,arial' size=-1>".$fetch["firstname"]."</td>
                <td><font face='verdana,arial' size=-1>".$fetch["lastname"]."</td>                  
                <td><font face='verdana,arial' size=-1>".$fetch["email"]."</td>
                <td><font face='verdana,arial' size=-5>".$fetch["salt"]."</td>";

                    if($fetch["account_status"] == 0){
                        echo "<form action='' method='post'>
                             <td><input type='submit' name='activate' value='Activate'></td>
                             </form>";
                    }
                    else{
                        echo "<td><font face='verdana,arial' size=-1>".$fetch["account_status"]."</td>";
                    }
    }

if (isset($_POST['activate'])){
    mysql_query("UPDATE accounts SET account_status=1 WHERE accountid='$accountupdate'") or die(mysql_error());
    header("Location:admin_accounts.php");
}
?>

add a hidden field with the id to be updated to your form:

if($fetch["account_status"] == 0){
    echo "<form action='' method='post'>
        <td><input type='submit' name='activate' value='Activate'>
        input type='hidden' name='upd_id' value='{$fetch['accountid']}'</td>
        </form>";
}

then after submit, read the id from the hidden field:

if (isset($_POST['activate'])){
    mysql_query("UPDATE accounts SET account_status=1 WHERE accountid='{$_POST['ups_id']}' LIMIT 1") or die(mysql_error());
    header("Location:admin_accounts.php");
}

Instead of using several forms you can use hyperlinks.Its needs little bit of code change

<td><input type='submit' name='activate' value='Activate'></td>
 TO 
 <td><a href='yourfilename.php?acctid='.$accountupdate >Activate</a></td>


      if (isset($_POST['activate'])){
      TO
      if (isset($_GET['acctid'])){
     $acctid = $_GET[acctid];
     mysql_query("UPDATE accounts SET account_status=1 WHERE accountid='$acctid '")         or            

die(mysql_error()); }

Finally as suggested change the code to mysqli* :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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