Hi all,
I have a table for users in a mysql database.
table_users
-id
-firstname
-lastname
-email
-password
-enabled
When I select and display records from the table, I want to add and enable/disable toggle button to the enabled field so that if its enabled and I click on it, it will update that individual record with the value '0' and if disabled, when I click on it it will update the record to '1' so that it becomes enabled. but this update has has to affect just the single record for the user in the database table.
Below is my select query, assuming we are already connected to the database.
<form name="update" method="POST" >
<table>
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Password</td>
<td>Statues</td>
</tr>
<?
// Loop through the table and display all records in tabular format
$query = "SELECT * FROM users";
$result = mysql_query ($query);
$count = mysql_num_rows($result); // Count table rows
while ($row = mysql_fetch_array($result))
{
$id = $row["id"];
$firstname = $row["firstname"];
$lastname = $row["lastname"];
$email = $row ["email"];
$password = $row ["password"];
$enabled = $row ["enabled"];
?>
<tr>
<td>><?php echo $id; ?></td>
<td><?php echo $firstname; ?></td>
<td><?php echo $lastname; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $password; ?></td>
<input type="hidded" name="enabled" />
<td><input type="image" src="status-toggle_<?php if ($enabled == '1') { echo 'enabled'; } else { echo 'disabled';}?>.png"
border="0" name="enabled" value="<?php echo $enabled; ?>"/></td>
</tr>
<?php // Close our while loop
}
?>
</table>
</form>
We are using two images to show the current state of the record in the database, If the column value for a record is 1 it will display the status-toggled_enabled.png image and if the coloum value is 0 it will display the statues-toggle_disabled.png image on the record.
Now I want to create toggle button so that if its clicked it will update the record to change its statues, say if it was enabled it will update it to '0' and switch to disabled, and vice versa.
So I begin the update statements like below and got stock
<?php
if (isset($_POST['enabled'])) {
$id = $_POST['id'];
$enabled = $_POST['enabled'];
}
?>
The first problem I am having is that I don't know how to update a single record, can someone help with the idea on how to complete my code. thank you