2/1/19

PHP & MySQli Coding References - Part 4 | Update Record


This is my part -4 php tutorial. before we have looked How to connect the database and view the data'show to insert the data as well as  how to delete record  in php using mysqli.

In this tutorial, We gonna learn How to delete the record in php mysqli

7. How to delete the data in a table
(We have two pages)
1. index.php (it is a interface)
2. process.php (it is a part of coding file)

Step 1: (index.php)
<?php //Database Connection ?>
<?php $mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli)); ?>

<?php //includes files ?>
<?php require_once 'process.php'; ?>


<?php if(isset($_SESSION['message'])): ?>
<div class="alert alert-<?=$_SESSION['msg_type'] ?>">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>

<?php endif; ?>


<div class="row justify-content-center">
<form action="process.php" method="POST">

<input type="hidden" name="id" value="<?php echo $id; ?>"/>

<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" value="<?php echo $name; ?>" placeholder="Enter your name"/>
</div>

<div class="form-group">
<label>Location:</label>
<input type="text" name="location" class="form-control" value="<?php echo $location; ?>" placeholder="Enter your location"/>
</div>

<div class="form-group">
<?php if($update == true): ?>
<button class="btn btn-info" type="submit" name="update">Update</button>
<?php else: ?>
<button type="submit" class="btn btn-primary" name="save">Save</button>
<?php endif; ?>
</div>

</form>

</div>



Step 2: (process.php)
<?php

session_start();

$mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli));

$id=0;
$update=false;
$name="";

$location="";

if(isset($_POST['update'])){
$id= $_POST['id'];
$name= $_POST['name'];
$location = $_POST['location'];

$mysqli->query("UPDATE data SET name='$name', location='$location' WHERE id='$id'") or die(mysqli_error($mysqli));

$_SESSION['message']= "Record has been updated!";
$_SESSION['msg_type']= "warning";

header("location: index.php");

}


?>

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *