PART 2 : Php Add Edit Delete Update Example for freshers

By May 4, 2012 Jquery, PHP
1. Use Part 1 instruction for table and database
 
2. Create  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=””>

  <script type=”text/javascript” src=”http://code.jquery.com/jquery-latest.js”></script>
  <script type=”text/javascript” src=”http://jzaefferer.github.com/jquery-validation/jquery.validate.js”></script>

  <script type=”text/javascript”>

$(document).ready(function() {  

    $(“#frm”).validate({

        rules: {
            name: “required”,
            email: “required”,          
            query: “required”,       

            name: {
                required: true
            },

            phone: {
                required: true
            },
            email: {
                required: true,
                email: true
            }

        },

        messages: {          

            name: {
                required: “Please enter Name”
            },
            email: “Please enter a valid email address”,
            query: “Please enter a valid query”          

        }

    });

});

</script>

 </head>
<style type=”text/css”>
    .error {
        color: red;       
        font-style: italic;
        margin-left: 10px;
        width: auto !important;
    }
    #lab{
        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’]; ?>”>
        <span id=”lab”>Name</span>
        <input type=”text” name=”name” id=”name” value=”<?php echo $arrquery[‘name’]; ?>”><br>
        <span id=”lab”>Email</span>
        <input type=”text” name=”email” id=”email” value=”<?php echo $arrquery[’email’]; ?>”><br>
        <span id=”lab”>Phone</span>
        <input type=”text” name=”phone” id=”phone” value=”<?php echo $arrquery[‘phone’]; ?>”><br>
        <span id=”lab”>Query</span>
        <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>

Leave a Reply