edit operation


Edit action is a bit complicated one. First you need to get the individual record-set using select query, display it in the form field and then use the update statement of SQL to replace the existing values with the new ones.

Let's add the link in edit icon to send to the edit file.

PASSING DATA ID TO THE UPDATE FILE USING GET METHOD VIA URL

<a href="edit.php?id=<?=$data['id']?>"><i class="fa fa-edit"></i></a>	

Here too, the primary id will be added respective to the record-set. So, any icon we click, the primary id of that recordset will be sent to the url.

MYSQLI QUERY TO DISPLAY EXISTING DATA IN THE UPDATE FORM FIELDS

Let's create a new file then to update the record-set. That new file will basically be similar to the insert form but will have values of that record-set in all fields fetched and displayed from the database.

<?php
// check for id in url and also check if the id value is not empty
if(isset($_GET['id']) && !empty($_GET['id'])){
	
	//get the id
	$id = (int)$_GET['id'];

	//SQL select statement
	$query = "SELECT * FROM user_info WHERE id = " .$id;

	//query execution
	$result = mysqli_query($connect,$query);

	// store associative array in some variable
	$data = mysqli_fetch_assoc($result);

	// explode multiple values from language column and store it as array
	$language = explode(',',$data['language']);
}else{
	header('Location: insert.php');
}
?>	

Here, we retrieved the id from the url and fetched associated data using sql statement. The language value will be displayed as a string as we imploded the array value and converted it to string earlier. So, we need to convert it back to array to display in the form fields. That's why we used the exploade function using ',' as key and restored hem to array. Now, we can display those values in respective form fields as shown below.

CREATE FORM FIELD TO DISPLAY EXISTING DATA

<div class="container">
	<form method="POST" action="editaction.php">
		<div class="col-sm-4 col-sm-offset-4">
				<h4><i class="fa fa-edit"></i> Edit User</h4><hr>
				<?php if(isset($_SESSION['error_message'])) : ?>
					<div class="alert alert-danger">
						<?= $_SESSION['error_message'] ?>
						<?php unset($_SESSION['error_message']); ?>
					</div>
				<?php endif ; ?>
				<?php if(isset($_SESSION['success_message'])) : ?>
					<div class="alert alert-success">
						<?= $_SESSION['success_message'] ?>
						<?php unset($_SESSION['success_message']); ?>
					</div>
				<?php endif ; ?>
				<input type="hidden" name="id" value="<?=$id?>">
				<div class="form-group">
					<label>Username</label>
					<input type="text" value="<?= $data['username']?>" name="username" class="form-control" required="">
				</div>
				<div class="form-group">
					<label>Email</label>
					<input type="email" value="<?=$data['email']?>" name="email" class="form-control" required="">
				</div>
				<div class="form-group">
					<label>Gender</label>
					<label for="male">
						<input type="radio" name="gender" <?= ($data['gender'] == 'male') ? 'checked=""' : '' ; ?> value="male"> Male
					</label>
					<label for="female"><input type="radio" name="gender" <?= ($data['gender'] == 'female') ? 'checked=""' : '' ; ?> value="female"> Female</label>
				</div>
				<div class="form-group">
					<label>Language</label>
					<label for="nep"><input type="checkbox" <?= (in_array('nep',$language)) ? 'checked=""' : '' ; ?> name="language[]" value="nep"> Nepali</label>
					<label for="eng"><input type="checkbox" <?= (in_array('eng',$language)) ? 'checked=""' : '' ; ?> name="language[]" value="eng"> English</label>
					<label for="oth"><input type="checkbox" <?= (in_array('oth',$language)) ? 'checked=""' : '' ; ?> name="language[]" value="oth"> Others</label>
				</div>
				<div class="form-group">
					<label>Country</label>
					<select name="country" class="form-control">
						<option <?= ($data['country'] == 'nepal') ? 'selected=""' : '' ; ?> value="nepal">Nepal</option>
						<option <?= ($data['country'] == 'others') ? 'selected=""' : '' ; ?> value="others">Others</option>
					</select>
				</div>
				<div class="form-group">
					<button class="btn btn-sm btn-success">Update</button>
				</div>
		</div>
	</form>
</div>		

Here, we printed the values from database record-set in their respective fields. For multiple options in the form, we used if statement to keep them selected if the corresponding values are present in the database and for that we used shorthand for if else statement.

Once the form is submitted with any of those values replaced we'll send those data to editaction field where we'll process the data as we did in insertaction.php. The only difference would be that, we'll use UPDATE statement instead of INSERT for which we'll need the primary id of the recordset. That's why, we added a hidden field in the form that'll automatically send the primary id of the recordset along with the data.

Now, let's go to the editaction.php file and have a look at the block of codes we need to execute.

BASIC PHP CODE TO UPDATE EXISTING DATA

<?php
// database connection
require_once('connect.php');

// check for data existence
if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
	// set each data in variables
	$id = $_POST['id'];
	$user = htmlspecialchars(trim($_POST['username']));
	$email = $_POST['email'];
	$gender = $_POST['gender'];
	$language = implode(',',$_POST['language']);
	$country = $_POST['country'];

	// update statement
	$query = "UPDATE user_info SET username = '$user', email = '$email', gender = '$gender', language = '$language', country = '$country' WHERE id =" .$id; 

	// statement execution
	$result = mysqli_query($connect,$query); 

	// set message in session to display to the user after redirecting
	if($result){
		$_SESSION['success_message'] = 'User data updated successfully';
		header('Location:insert.php');
		exit();
	}else{
		$_SESSION['error_message'] = 'Error encountered while updating';
		header('Location:insert.php');
		exit();
	}
}	

So, we fetched the connection to the database, checked the data existence and stored each of the posted values in variables. Then we created the SQL UPDATE statement and executed it. Once the execution took place, we set the message in session to display to the users as they are redirected to the insert.php file either the data has been updated or not.

This way, we can create multiple data tables with required data columns and carry out those INSERT, DISPLAY, EDIT and DELETE operations easily with Procedural PHP.