I've created 3 pages in php,
1) admin_details.php 2). edit_admin_details.php 3). edit_admin_details_process.php
The admin_details.php page selecting all admins data, the edit_admin_details.php selecting the admin details for editing/updating with $admin_id=_GET['id'] from admin_details.php page and the edit_admin_details_process.php updates the data .
now my problem is that i want some php form validation for edit_admin_details.php , so i search the internet, i found the solution that use session variables with header() $_SESSION['data']=_POST
Which will obviously create 2 dimension array of all variables of form like $_SESSION['data']['admin_id']
etc or use edit_admin_details_process on the same page. i use the session variables to validate it . but when i click update button after changing some data in the input fields , the page action=edit_admin_details.php to check for validation so i get undefined index error for $_GET['id'].i tried 5 hours to find a solution but in vain. please help me get out of the problem. i am doing my final year project. The code is written below . . . .
1). admin_details.php
<?php
session_start();
if(isset($_SESSION['admin_username'])){
include("../include/connection.php");
$query = "SELECT * FROM `admins`";
$result = mysqli_query($conn,$query);
}else{
header("Location: login.php");
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Admin Details</title>
</head>
<body>
<table border="1" align="center">
<thead>
<th>Admin Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Delete</th>
<th>Edit Details</th>
</thead>
<?php
while($row=mysqli_fetch_assoc($result)){
echo("<tr>");
echo("<td>".$row['ADMIN_ID']."</td>");
echo("<td>".$row['FIRST_NAME']."</td>");
echo("<td>".$row['LAST_NAME']."</td>");
echo("<td>".$row['EMAIL']."</td>");
echo("<td>".$row['PHONE']."</td>");
echo("<td><a href='"."delete_admins.php?id=".$row['ADMIN_ID']."'>Delete</a></td>");
echo("<td><a href='"."edit_admin_details.php?id=".$row['ADMIN_ID']."'>Edit</a></td>");
echo("</tr>");
}
?>
</table>
</body>
</html>
2). edit_admin_details.php
<?php
session_start();
if(isset($_SESSION['admin_username'])){
include("../include/connection.php");
include("../include/functions.php");
if(!isset($_GET['id'])){
header("Location: index.php");
}
$admin_id = mysqli_real_escape_string($conn,$_GET['id']);
$querySelect = "SELECT * FROM `admins` WHERE ADMIN_ID='$admin_id'";
$resultSelect = mysqli_query($conn,$querySelect) or die("unable to query ".mysqli_error($conn));
$row = mysqli_fetch_assoc($resultSelect);
}else{
header("Location: login.php");
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit Admin Details</title>
</head>
<body>
<form action="edit_admin_details_process.php" method="post">
Admin Id :
<input type="text" name="admin_id" id="admin_id" value="<?php echo $row['ADMIN_ID']; ?>"><br>
Admin First Name:
<input type="text" name="first_name" id="first_name" value="<?php echo $row['FIRST_NAME']; ?>"><br>
Admin Last Name:
<input type="text" name="last_name" id="last_name" value="<?php echo $row['LAST_NAME']; ?>"><br>
Admin Email:
<input type="email" name="email" id="email" value="<?php echo $row['EMAIL']; ?>"><br>
Mobile Number:
<input type="number" name="mobile_number" id="mobile_number" value="<?php echo $row['PHONE']; ?>"><br>
<button type="submit" name="submit" id="submit">UPDATE</button>
</form>
</body>
</html>
3). edit_admin_details_process.php
<?php
session_start();
if(isset($_SESSION['admin_username'])){
include("../include/connection.php");
if(!isset($_POST['admin_id'])){
header("Location: index.php");
}
$admin_id = mysqli_real_escape_string($conn,$_POST['admin_id']);
if(isset($_POST['submit'])){
$first_name = strtolower(trim($_POST['first_name']));
$last_name = strtolower(trim($_POST['last_name']));
$email = strtolower(trim($_POST['email']));
$mobile_number = $_POST['mobile_number'];
$queryUpdate = "UPDATE `admins` SET `FIRST_NAME`='$first_name',`LAST_NAME`='$last_name',`EMAIL`='$email',`PHONE`='$mobile_number' WHERE ADMIN_ID='$admin_id'";
$resultUpdate = mysqli_query($conn,$queryUpdate);
if($resultUpdate){
header("Location: admin_details.php");
}
}
}else{
header("Location: login.php");
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
</body>
</html>