Friday, November 15, 2013

Modify Data into Database ( Fetch Edit and Delete ) code also included i this tutorial


Basic Fetch, Edit, Update, Delete commands in PHP.

Step :1. Make database of college in phpmyadmin.

Step:2. Create table student

Step:3. Create columns  s_id, s_name, s_class.
(Note:- Make auto increment for s_id)

Step:4. Make a form in HTML.

//  Make a file student.php

<!DOCTYPE html>
<html>
<body>

<form action="insert.php" method="post">  //*Attached insert.php file with this form
Name: <input type="text" name="name"><br>
Class: <input type="text" class="class"><br>
<input type="submit" value="submit" />
</form>
</body>
</html>

Coding for insert data dynamically in this form:-

Make file insert.php.


<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("college", $con);

$sql="INSERT INTO student (name, class)
VALUES
('$_POST[name]','$_POST[class]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>


Coding for Fetch data dynamically in this form:-

Step:1. Make a file config.php.


<?php
error_reporting(0);
$con = mysql_connect("localhost","root","");
mysql_select_db("college");
?>

Step:2. Make a file fetchdata.php.
<?php
include("config.php");
$select = mysql_query("select * from student");
?>
<table border="2">
<tr>
<td>s_id</td>
<td>s_name</td>
<td>s_class</td>
<td colspan="2">Operation</td>
</tr>
<?php
while($row=mysql_fetch_array($select))
{
?>
<tr> <td> <?php               echo $row['s_id']."<br>"; ?> </td>
 <td> <?php       echo $row['s_name ']."<br>"; ?> </td> 
 <td> <?php       echo $row['s_class']."<br>"; ?> </td>
 <td>  <a href="edit.php?
 's_id =<?php echo $row[''s_id ']?>&
 s_name =<?php echo $row[s_name ']?>&
  s_class =<?php echo $row[' s_class ']?>"> Edit </a></td>
 <td> <a href="delete.php?s_id=<?php echo $row[s_id]?>">Delete</a> </td> <?php

 </tr>
<?php
}
?>
(Note:-  We attached edit.php and delete.php files in Edit and Delete for Edit and Delete in this form)



// Coding for Edit data dynamically in this form:-

// Make file edit.php.


<?php
$s_id=$_GET[' s_id '];
$s_name=$_GET[' s_name '];
$s_class =$_GET['s_class'];
?>

<form action="update.php" method="post">  //*Attached update.php file to update in this form.
<table border="2">
<tr>
<td>s_id</td>
<td> s_name </td>
<td> s_class </td>
</tr>

<tr>
<input type="hidden" name="s_id" value="<?php echo $ s_id; ?>" >
<td><input type="text" > s_name ="id" value="<?php echo $> s_name; ?>" ></td>
<td><input type="text" s_class ="class" value="<?php echo $ s_class; ?>" ></td>
</tr>

<tr>
<td colspan="3" align="center"><input type="submit" ></td>
</tr>

</table>
</form>



Coding for Update data dynamically in this form:-

Make file update.php.

<?php
include("config.php");


$s_id =$_GET[' s_id '];
$s_name =$_GET[' s_name '];
$s_class=$_GET[s_class];

if(!empty($s_name))
{
                $update=mysql_query("update student set s_name ='$ s_name where s_ id='$s_id'");
}

if(!empty($s_class))
{
                $update=mysql_query("update student set s_class ='$ s_class where s_ id='$s_ id'");
}
header("location:fetchdata.php");//*//*Attached fetchdata.php file here to show files after update.   
?>


  
Coding for Delete data dynamically in this form:-

 Make delete.php

<?php include("config.php");

$s_id=$_GET[' s_id '];
$delete = mysql_query("delete from student where s_id ='$ s_id '");
if($delete)
{
header("location:fetchdata.php");  //*Attached fetchdata.php file here to show files after delete
}
else
{
echo "error";
}
?>










































3 comments:

Unknown said...

Thanks sir,these notes really helped me for doing php.....

Unknown said...

Great resource for learning PHP.

Unknown said...

easily understand each one...thank a lot sir.....