I created a display page using php
where all users records are displayed in a table and there are delete and edit buttons to manage users, for the delete option I managed to do it but it's been hours since yesterday trying to do the edit button. I want all the user's data (full_name, email, password...city) to be passed from the display.php
page to the update.php
, and I want the new data that will be updated to be keyed in by a form which I made it as updateform.html
but I got studck on how to connect these 3 files to successfully update the users detail.
Here are the files:
display.php
<!DOCTYPE html>
<html>
<head>
<style>
table,
td,
th {
border: 1.5px solid #d9d9d9;
border-collapse: collapse;
}
table {
width: 100%;
}
</style>
</head>
<body>
</div>
<div class="container">
<?php
$conn = mysqli_connect("localhost", "root", "", "finalproject") or die("<script>alert('Connection Failed.')</script>");
$sql = "SELECT * FROM users" or die("<script>alert('Connection Failed.')</script>");
$result = mysqli_query($conn, $sql);
?>
<div class="row justify-contect-center">
<table class="table" border-size="1">
<thead>
<tr>
<th>Username</th>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
<th>Age</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Work Duration</th>
<th>IG Account</th>
<th>State</th>
<th>Postcode</th>
<th>City</th>
<th>Action</th>
</tr>
</thead>
<?php
//while($row = $result->fetch_assoc());
while ($row = $result->fetch_assoc()) :
?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['full_name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['password'] ?></td>
<td><?php echo $row['age'] ?></td>
<td><?php echo $row['gender'] ?></td>
<td><?php echo $row['phone_number'] ?></td>
<td><?php echo $row['work_duration'] ?></td>
<td><?php echo $row['ig_account'] ?></td>
<td><?php echo $row['state'] ?></td>
<td><?php echo $row['postcode'] ?></td>
<td><?php echo $row['city'] ?></td>
<td>
<a href="updateform.html?edit=<?php if(isset($username)){
$conn->query("Select * from users where username='$username'");
} ?>" class="btn btn-info">Edit</a>
<a href="delete.php?delete=<?php echo $row['username']; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php
function pre_r($array)
{
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
</body>
</html>
updateform.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Update Information</title>
</head>
<body>
<div class="container">
<form action="update.php" method="POST" class="login-email">
<p class="login-text" style="font-size: 2rem; font-weight: 800;">Update Information</p>
<div class="input-group" style="margin-bottom: 40px;">Full Name:
<input type="text" name="full_name" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Email:
<input type="email" name="email" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Password:
<input type="password" name="password" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Confirm Password:
<input type="password" name="cpassword" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Age:
<input type="text" name="age" value="" required>
</div>
<div style="margin-top: 40px; margin-bottom: 20px;">Gender:
<label for="f-option" class="l-radio" value="">
<input type="radio" name="gender" tabindex="1" value="Male" style="margin-left:10px;">
<span> Male</span>
<input type="radio" name="gender" tabindex="2" value="Female" style="margin-left:20px;">
<span> Female</span>
</div>
<div class="input-group" style="margin-bottom: 40px;">Phone Number:
<input type="text" name="phone_number" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Work Duration:
<input type="text" name="work_duration" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">IG Account:
<input type="text" name="ig_account" value="" required>
</div>
<div class="form-group" style="margin-bottom: 10px;">
<label>State:</label>
<select name="state" class="form-control" value="" required>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
<div class="input-group" style="margin-bottom: 40px;">Postcode:
<input type="text" name="postcode" value="" required>
</div>
<div class="input-group" style="margin-bottom: 50px;">City:
<input type="text" name="city" value="" required>
</div>
<div class="input-group" style="margin-top:40px;">
<button name="update" class="btn">Update</button>
</div>
</form>
</div>
</body>
</html>
update.php
<?php
$conn = mysqli_connect("localhost", "root", "", "finalproject") or die("<script>alert('Connection Failed.')</script>");
if (isset($_POST['update'])) {
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
$phone_number = $_POST['phone_number'];
$work_duration = $_POST['work_duration'];
$ig_account = $_POST['ig_account'];
$state = $_POST["state"];
$postcode = $_POST['postcode'];
$city = $_POST['city'];
$gender = $_POST['gender'];
$age = $_POST['age'];
}
?>