Friday, March 17, 2017

add edit display in php

Add update and display

Config.php

<?php
$databaseHost = 'localhost';
$databaseName = 'test';
$databaseUsername = 'root';
$databasePassword = 'root';
 
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 
?>

ADD.HTML
<html>
<head>
    <title>Add Data</title>
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>

    <form action="add.php" method="post" name="form1">
        <table width="25%" border="0">
            <tr>
                <td>Name</td>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <td>Age</td>
                <td><input type="text" name="age"></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="Submit" value="Add"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Add.php


<html>
<head>
    <title>Add Data</title>
</head>

<body>
<?php
//including the database connection file
include_once("config.php");

if(isset($_POST['Submit'])) {  
    $name = $_POST['name'];
    $age = $_POST['age'];
    $email = $_POST['email'];
       
    // checking empty fields
    if(empty($name) || empty($age) || empty($email)) {              
        if(empty($name)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }
       
        if(empty($age)) {
            echo "<font color='red'>Age field is empty.</font><br/>";
        }
       
        if(empty($email)) {
            echo "<font color='red'>Email field is empty.</font><br/>";
        }
       
        //link to the previous page
        echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
    } else {
        // if all the fields are filled (not empty)            
        //insert data to database
        $result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");
       
        //display success message
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";
    }
}
?>
</body>
</html>


EDIT.php


<?php
// including the database connection file
include_once("config.php");

if(isset($_POST['update']))
{  
    $id = $_POST['id'];
   
    $name=$_POST['name'];
    $age=$_POST['age'];
    $email=$_POST['email'];  
   
    // checking empty fields
    if(empty($name) || empty($age) || empty($email)) {          
        if(empty($name)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }
       
        if(empty($age)) {
            echo "<font color='red'>Age field is empty.</font><br/>";
        }
       
        if(empty($email)) {
            echo "<font color='red'>Email field is empty.</font><br/>";
        }      
    } else {  
        //updating the table
        $result = mysqli_query($mysqli, "UPDATE users SET name='$name',age='$age',email='$email' WHERE id=$id");
       
        //redirectig to the display page. In our case, it is index.php
        header("Location: index.php");
    }
}
?>


<?php
//getting id from url
$id = $_GET['id'];

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");

while($res = mysqli_fetch_array($result))
{
    $name = $res['name'];
    $age = $res['age'];
    $email = $res['email'];
}
?>
<html>
<head>  
    <title>Edit Data</title>
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>
   
    <form name="form1" method="post" action="edit.php">
        <table border="0">
            <tr>
                <td>Name</td>
                <td><input type="text" name="name" value="<?php echo $name;?>"></td>
            </tr>
            <tr>
                <td>Age</td>
                <td><input type="text" name="age" value="<?php echo $age;?>"></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="email" value="<?php echo $email;?>"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
</body>
</html>


INDEX.PHP


<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); // using mysqli_query instead
?>

<html>
<head>    
    <title>Homepage</title>
</head>

<body>
    <a href="add.html">Add New Data</a><br/><br/>

    <table width='80%' border=0>
        <tr bgcolor='#CCCCCC'>
            <td>Name</td>
            <td>Age</td>
            <td>Email</td>
            <td>Update</td>
        </tr>
        <?php 
        //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 
        while($res = mysqli_fetch_array($result)) {         
            echo "<tr>";
            echo "<td>".$res['name']."</td>";
            echo "<td>".$res['age']."</td>";
            echo "<td>".$res['email']."</td>";    
            echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a></td></tr>";        
        }
        ?>
    </table>
</body>

</html>

2 comments:

  1. PHP – Hypertext preprocessor is a general purpose scripting language specially designed for the web development that is easily embedded into HTML language. Some of the excellent features in PHP include capability to create dynamic page content, send and receive cookies, encrypt data.
    Regards:
    PHP Training Chennai
    PHP Training Institute in Chennai

    ReplyDelete
  2. Your Web Designing article impressed me to take up Web Designing Course. I am a regular reader of your blog. Keep doing more and I am waiting for your next blog.
    Regards:
    web designing training in chennai
    web design training in chennai

    ReplyDelete