PHP (Core & Framework)

How to modify your data using mysql

In this tutorial you will learn the following things :-

  • Deleting your record
  • Edit your record

How to Delete a particular record

To delete a particular record from your data you need to pass a sql query :-

$sql= “delete from myrecords where item_id=”.$_GET[‘id’];”

GET is used to request data from a specified resource.

Now using the function ‘mysqli_query($conn,$sql)’

across the row, also pass connection file and sql query as its arguments.

If this function is executed then your data is deleted successfully !

header(“Location:show.php”);

Header is used to take you to the page where all your records are present this done once the record is deleted !!

Records before deletion
Records after deletion
<?php 

include("connectins.php");

$sql="delete from myrecords where item_id=".$_GET['id'];

if(mysqli_query($conn,$sql))
{
	echo "<br> Record Deleted Successfully !";
	header("Location:show.php");
}
?>

How to Edit your record 

To edit your records you need to pass sql UPDATE query. The UPDATE statement is used to update existing records in a table.

$sql=”update myrecords set  title=’$ti’,startdate=’$sd’,enddate=’$ed’,postdate=’$pd’ where item_id=$id”;

This query runs to add new details to the existing ones.

Once you click the edit button a new form is opened to edit the required details.

TITLE:<input type=”text” name=”title” value=”<?php echo $row[‘title’];?>”><br>

As you all must be knowing that how to create a form here you see that in input tag we have specified the value of each one. This is done because the user would like to see the previous data and would be able to edit the required data according to his need.

<form name=f1 method=post action=”>

<input type=”hidden” name=”item_id” value=”<?php echo $_GET[‘id’];?>”>

Here type is hidden because The <input type=”hidden”> defines a hidden input field.

A hidden field let web developers include data that cannot be seen or modified by users when a form is submitted.

A hidden field often stores what database record that needs to be updated when the form is submitted.

Rest function are described in previous tutorials.

Records Before Editing

Records after editing

<?php

include("connectins.php");
if(isset($_POST['submit']))
{

 	$id=$_POST['item_id'];
 	$ti=$_POST['title'];
	$sd=$_POST['s'];
	$ed=$_POST['e'];
	$pd=$_POST['p'];

 $sql="update myrecords set  title='$ti',startdate='$sd',enddate='$ed',postdate='$pd' where item_id=$id";

 if(mysqli_query($conn,$sql))
 {
 	echo "Updated Successfully !!!";
 	
 }
 else
 {
 	echo "There was some error!!";
 }
}
else
{
	$sql="select * from myrecords where item_id=".$_GET['id'];
	echo $sql;
$rs=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($rs);
?>
	<form name=f1 method=post action=''>
		<input type="hidden" name="item_id" value="<?php echo $_GET['id'];?>">
		TITLE:<input type="text" name="title" value="<?php echo $row['title'];?>"><br>
		START DATE:<input type="Date" name="s"value="<?php echo $row['startdate'];?>"><br>
		END DATE:<input type="Date" name="e"value="<?php echo $row['enddate'];?>"><br>
		POST DATE:<input type="Date" name="p"value="<?php echo $row['postdate'];?>"><br>
		<input type="submit" name="submit" value="Update">
	
 </form>
 <?php
 }

 ?>

Posted by: Rhythm Arya

Comment here

50 − = 46