I have a form that sends information from a user database to a $_POST function. I got the information to pass the values to the function, but it's only sending the last entry from the database column. For example I'm trying to update the database row with an ID of '1', but instead it keeps updating the row with an ID '9'. Inside the form there is a table which displays all the user information using a foreach loop, and using inputs, the information is passed when a button is pressed. All the other information passes fine, just not the ID.
I think it caused by the form being outside the foreach loop, making It only send the last value it receives. The code will be posted below and I'm mainly wondering if anyone knows of any workarounds for using forms and tables together? or is it just a case of using lots of css.
The foreach loop inside the form uses a variable which gets its info from a sql function:
//calls the function at the top of the page
$userData = getUserProf();
///////////////////////////
// function to return all the data from the user database
function getUserProf() {
global $conn;
// select data from user database
$sql = "SELECT * FROM users";
$userData = mysqli_query($conn, $sql);
$getUserData = mysqli_fetch_all($userData, MYSQLI_ASSOC);
// return user data
return $getUserData;
}
///////////////////////////
Form
<form action="includes/adminActions.scr.php" method="POST">
<table class="ui padded celled table">
<thead>
<tr><th>UserID</th>
<th>Username</th>
<th>Email</th>
<th>Password</th>
<th>User Role</th>
<th>Actions</th>
</tr></thead>
<tbody>
<?php foreach ($userData as $user): ?>
<tr>
<th><div class="ui fluid disabled transparent input"><input name="userId" placeholder="<?php echo htmlentities($user['userID']) ?>" value="<?php echo $user['userID']?>"/></div></th>
<th><div class="ui input"><input name="username" placeholder="<?php echo htmlentities($user['username']) ?>"/></div></th>
<th><div class="ui input"><input name="email" placeholder="<?php echo htmlentities($user['email']) ?>"/></div></th>
<th><div class="ui input"><input name="password" placeholder="<?php echo htmlentities($user['password']) ?>"/></div></th>
<th><h3><?php echo htmlentities($user['user_lvl']) ?></h3></th>
<th><button type="submit" class="ui small blue button" name="updateUser">Update</button>
<a href="includes/adminActions.scr.php?makeAdmin=<?php echo $user['userID']?>" class="ui small green button">Lvl Up</a>
<a href="includes/adminActions.scr.php?deleteUser=<?php echo $user['userID']?>" class="ui small red button">Delete</a>
<a href="includes/adminActions.scr.php?makeUser=<?php echo $user['userID']?>" class="ui small orange button">Lvl Down</a>
</th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
And then finally, here is the two parts from the actions script:
// if user clicks the update button
if (isset($_POST['updateUser'])) {
updateUser($_POST);
}
// take in POST variables and update the database
function updateUser($postValues) {
global $conn, $userID, $username, $email, $password;
$userID = ($postValues['userId']);
$username = ($postValues['username']);
$email = ($postValues['email']);
$password = ($postValues['password']);
$hashedPass = password_hash($password, PASSWORD_DEFAULT);
$sql = "UPDATE users SET username = '$username', email = '$email', password = '$hashedPass' WHERE userID = '$userID'";
if (mysqli_query($conn, $sql)) {
header("location: ../editUsers.php?isEditing=1&userID=$userID");
exit();
} else {
header("Location: ../editUsers.php?mysqlerror");
exit();
}
}