PART 1 : Php Add Edit Delete Update Example for freshers

1. Create  Database: 'sampletest'


Create Table structure for table 'user'


CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) NOT NULL,
  `email` varchar(250) NOT NULL,
  `phone` varchar(250) NOT NULL,
  `query` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
)

2. register.php

<?php

$con = mysql_connect("localhost","root","");

if (!$con){
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("sampletest", $con);



if($_GET['type'] == 'delete'){
    $delquery = "delete from user where id='" . $_GET['id'] . "'";
    mysql_query($delquery);

}else if($_GET['type'] == 'update'){

    $selquery = "select * from user where id='" . $_GET['id'] . "'";
    $rsquery = mysql_query($selquery);
    $arrquery = mysql_fetch_array($rsquery);


}

if($_POST['submitbutton'] == 'Add'){

    $name= $_POST['name'];
    $email= $_POST['email'];
    $phone= $_POST['phone'];
    $query= $_POST['query'];

    $query = "insert into user (name, email, phone, query) values ('".$name."','".$email."' ,'".$phone."' ,'".$query."')";
    mysql_query($query);
   
}

if($_POST['submitbutton'] == 'Update'){
    $id = $_POST['hidd'];
    $name= $_POST['name'];
    $email= $_POST['email'];
    $phone= $_POST['phone'];
    $query= $_POST['query'];

    $query = "update user set name = '".$name."', email = '".$email."', phone='".$phone."', query='".$query."' where id='".$id."'";
    mysql_query($query);
   
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
<style type="text/css">
    label{
        float: left;
        width: 100px;
        font-weight: bold;
        padding:0;
    }
    input[type=text], textarea{
        width: 270px;
        height:25px;
        margin:5px;
        background-color:#e6e3dc;
        border:1px solid #d8d5d0;
        -webkit-border-radius: 7px;-moz-border-radius: 7px;border-radius: 7px;
    }

    textarea {
        height: 150px;
    }
    .submitbutton{

        -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px;
        background-color:#00c462;
        padding:3px 10px;
        font-size:12px;
        color:#FFF;

    }
</style>

 <body>
    <form name="frm" id="frm" action="" method="POST">
        <input type="hidden" id="hidd" name="hidd" value="<?php echo $arrquery['id']; ?>">
        <label>Name</label>
        <input type="text" name="name" id="name" value="<?php echo $arrquery['name']; ?>"><br>
        <label>Email</label>
        <input type="text" name="email" id="email" value="<?php echo $arrquery['email']; ?>"><br>
        <label>Phone</label>
        <input type="text" name="phone" id="phone" value="<?php echo $arrquery['phone']; ?>"><br>
        <label>Query</label>
        <textarea id="query" name="query"><?php echo $arrquery['query']; ?></textarea>

        <br>
        <label>&nbsp;</label>
        <?php if($_GET['type'] == 'update') { ?>
        <input type="submit" name="submitbutton" id="submitbutton" value="Update" class="submitbutton"/>
        <input type="submit" name="submitbutton" id="submitbutton" value="Add" class="submitbutton"/>
        <?php } else {?>
        <input type="submit" name="submitbutton" id="submitbutton" value="Add" class="submitbutton"/>
        <?php } ?>
    </form>
    <br>
    <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td><b>Name</b></td>
            <td><b>Email</b></td>
            <td><b>Phone</b></td>
            <td><b>Action</b></td>
        </tr>
    <?php

        $select = "select * from user";

        $rsSelect = mysql_query($select);

        while($arrSelect = mysql_fetch_array($rsSelect)){
    ?>
        <tr>
            <td><?php echo $arrSelect['name']; ?></td>
            <td><?php echo $arrSelect['Email']; ?></td>
            <td><?php echo $arrSelect['Phone']; ?></td>
            <td><a href='http://localhost/sampletest/register.php?type=update&id=<?php echo $arrSelect['id']; ?>'>Update</a> / <a href='http://localhost/sampletest/register.php?type=delete&id=<?php echo $arrSelect['id']; ?>'>Delete</a></td>   
        </tr>
    <?php } ?>
    </table>

 </body>
</html>

Most Popular

Connect to Amazon EC2 instance using Filezilla and SFTP

Pan Card Validation Using Javascript

Customize wordpress register form wp_register()