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 >
<?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 >
<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'");
} ?>" >Edit</a>
<a href="delete.php?delete=<?php echo $row['username']; ?>" >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 >
<form action="update.php" method="POST" >
<p style="font-size: 2rem; font-weight: 800;">Update Information</p>
<div style="margin-bottom: 40px;">Full Name:
<input type="text" name="full_name" value="" required>
</div>
<div style="margin-bottom: 40px;">Email:
<input type="email" name="email" value="" required>
</div>
<div style="margin-bottom: 40px;">Password:
<input type="password" name="password" value="" required>
</div>
<div style="margin-bottom: 40px;">Confirm Password:
<input type="password" name="cpassword" value="" required>
</div>
<div 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" 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 style="margin-bottom: 40px;">Phone Number:
<input type="text" name="phone_number" value="" required>
</div>
<div style="margin-bottom: 40px;">Work Duration:
<input type="text" name="work_duration" value="" required>
</div>
<div style="margin-bottom: 40px;">IG Account:
<input type="text" name="ig_account" value="" required>
</div>
<div style="margin-bottom: 10px;">
<label>State:</label>
<select name="state" value="" required>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
<div style="margin-bottom: 40px;">Postcode:
<input type="text" name="postcode" value="" required>
</div>
<div style="margin-bottom: 50px;">City:
<input type="text" name="city" value="" required>
</div>
<div style="margin-top:40px;">
<button name="update" >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'];
}
?>
CodePudding user response:
I can see a lot of issues with your code, so I went above and beyond to recode it for you and even did the update part for you and hopefully you learn a few tricks from it. I assume your fairly new to the game based on your code... I added extra files to make your life easier in long run.
*A few tips
- You don't need to use ?> to close php at the end of a file
<?=$username?>
is the same as<? echo $username; ?>
. Easy on eyes, simple.- Try not to use tables, I didn't go as far as messing with your but check out flex for tables or use div
- don't use border-size in html tables... try not to use style in anything in html... always use css. classes are used multiple times .classname and ID's are only used once #idname*
_config.php Put your db and other settings in this file to be included into all your pages so your not wasting your time writing the same thing over and over
<?php
$db = new mysqli('localhost', 'user', 'pass', 'riseofwar_v1');
if ($db->connect_errno > 0) {
die('Unable to connect to database [' . $db->connect_error . ']');
}
_funcs.php I'm giving you my basic library to use to sanitize your input fields
<?php
include('_config.php');
// BASIC FUNCTIONS; Carry this library to all your future projects too!
function abc ($input){ return preg_match('/^[A-Z] $/i', $input); }
function abcSpc ($input){ return preg_match('/^[a-z][a-z\ ]*$/i', $input); }
function abcNum ($input){ return preg_match('/^[A-Z0-9] $/i', $input); }
function abcNumSpc ($input){ return preg_match('/^[A-Z0-9\ ] $/i', $input); }
function abcNumU ($input){ return preg_match('/^[A-Z0-9_-] $/i', $input); }
function abcNumD ($input){ return preg_match('/^[A-Z0-9-] $/i', $input); }
function num ($input){ if(strlen($input) > 24){ $input=0; }if(!preg_match('/^[0-9] $/', $input)){ $input=0; } return $input; }
function numDot ($input){ if(strlen($input) > 24){ $input=0; }if(!preg_match('/^[0-9.] $/', $input)){ $input=0; } return $input; }
function numU ($input){ return preg_match('/^[0-9_-] $/i', $input); }
function is_odd($num){ return($num & 1); }
function email ($input){ return filter_var($input, FILTER_VALIDATE_EMAIL); }
function phone ($input){ $phone = preg_replace('/[^0-9]/', '', $input); if(strlen($phone) < 10 || !num($phone)) { $input=false; } return $input; }
function is_url($input){ return preg_match('/^(http|https):\/\/[a-z0-9] ([\-\.]{1}[a-z0-9] )*\.[a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i',$input); }
function is_uri ($input){ return preg_match('/^[a-z0-9-] $/i', $input); }
function findIt($find,$string){ return preg_match("/$find/i","$string"); }
_html.php You'll love this... modify your header and footer for every html page you create... This is the file you include in all your existing and new pages.
<?
include('_funcs.php');
// Simple function you can call on every page so you don't have to edit each file every time you change something in the headers
function head($title){?>
<!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><?=$title?></title>
</head>
<body>
<?
}
// Same as the above function, just the footer
function foot(){?>
</body>
</html>
<? }
display.php
<?php
include('_html.php');
head('Viewing Users');
?>
<div >
<div >
<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
$result = $db->query("SELECT * FROM users ORDER BY id LIMIT 25;");
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="edit.php?id=<?=$row['id']?>" >Edit</a>
<a href="delete.php?delete=<?php echo $row['username']; ?>" >Delete</a>
</td>
</tr>
<?}?>
</table>
</div>
<? foot();
edit.php
<?php
include('_html.php');
$id = num($_GET['id']) ?: false; // Check the ID in the URL to make sure it's a number
$user = $db->query("SELECT * FROM users WHERE id='$id'")->fetch_assoc();
if (isset($_POST['update'])) {
// CHECK VALUES BEFORE YOU UPDATE THEM
$user['full_name'] = abcSpc($_POST['full_name']) ? $_POST['full_name'] : $user['full_name'];
$user['email'] = email($_POST['email']) ? $_POST['email'] : $user['email'];
$user['password'] = abcNum($_POST['password']) ? $_POST['password'] : $user['password'];
$user['password'] = $user['password'] != $user['cpassword'] ? $user['password'] : $_POST['password'];
$user['phone_number'] = phone($_POST['phone_number']) ? $_POST['phone_number'] : $user['phone_number'];
$user['work_duration'] = num($_POST['work_duration']) ? $_POST['work_duration'] : $user['work_duration'];
$user['ig_account'] = abcSpc($_POST['ig_account']) ? $_POST['ig_account'] : $user['ig_account'];
$user['state'] = num($_POST["state"]) ? $_POST['state'] : $user['state'];
$user['postcode'] = abc($_POST['postcode']) ? $_POST['postcode'] : $user['postcode'];
$user['city'] = num($_POST['city']) ? $_POST['city'] : $user['city'];
$user['gender'] = num($_POST['gender']) ? $_POST['gender'] : $user['gender'];
$user['age'] = num($_POST['age']) ? $_POST['age'] : $user['age'];
$msg = 'Updated! <a href="display.php">View all users again</a>';
$db->query("UPDATE users SET full_name=$user[full_name], email=$user[email], password=$user[password], phone_number=$user[phone_number], work_duration=$user[work_duration], ig_account=$user[ig_account], state=$user[state], postcode=$user[postcode], city=$user[city], gender=$user[gender], age=$user[age] WHERE id='$_SESSION[id]';");
}
head('Update Information');
?>
<?=$msg?>
<div >
<form method="POST" >
<p style="font-size: 2rem; font-weight: 800;">Update Information</p>
<div style="margin-bottom: 40px;">Full Name:
<input type="text" name="full_name" value="<?=$user['full_name']?>" required>
</div>
<div style="margin-bottom: 40px;">Email:
<input type="email" name="email" value="<?=$user['email']?>" required>
</div>
<div style="margin-bottom: 40px;">Password:
<input type="password" name="password" value="">
</div>
<div style="margin-bottom: 40px;">Confirm Password:
<input type="password" name="cpassword" value="">
</div>
<div style="margin-bottom: 40px;">Age:
<input type="text" name="age" value="<?=$user['age']?>" required>
</div>
<div style="margin-top: 40px; margin-bottom: 20px;">Gender:
<label for="f-option" value="">
<input type="radio" name="gender" checked 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 style="margin-bottom: 40px;">Phone Number:
<input type="text" name="phone_number" value="<?=$user['phone_number']?>" required>
</div>
<div style="margin-bottom: 40px;">Work Duration:
<input type="text" name="work_duration" value="<?=$user['work_duration']?>" required>
</div>
<div style="margin-bottom: 40px;">IG Account:
<input type="text" name="ig_account" value="<?=$user['ig_account']?>" required>
</div>
<div style="margin-bottom: 10px;">
<label>State:</label>
<select name="state" required>
<option value="1" <?=$state==1?'checked':''?>>Option 1</option>
<option value="2" <?=$state==2?'checked':''?>>Option 2</option>
<option value="3" <?=$state==3?'checked':''?>>Option 3</option>
</select>
</div>
<div style="margin-bottom: 40px;">Postcode:
<input type="text" name="postcode" value="<?=$user['postcode']?>" required>
</div>
<div style="margin-bottom: 50px;">City:
<input type="text" name="city" value="<?=$user['city']?>" required>
</div>
<div style="margin-top:40px;">
<button name="update" >Update</button>
</div>
</form>
</div>
<?php foot();