Php: Insert Update Delete Search In MySQL Database Using Php

Php Add , Edit , Remove , Find In MySQL Database Using Php With Source Code


________________________________________________________


In this Php Tutorial we will see How To Insert Update Delete Search Data In MySQL Database Table From html From With Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .



Part 1


Part 2



Php Source Code:

<?php

$host = "localhost";
$user = "root";
$password ="";
$database = "test_db";

$id = "";
$fname = "";
$lname = "";
$age = "";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// connect to mysql database
try{
    $connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
    echo 'Error';
}

// get values from the form
function getPosts()
{
    $posts = array();
    $posts[0] = $_POST['id'];
    $posts[1] = $_POST['fname'];
    $posts[2] = $_POST['lname'];
    $posts[3] = $_POST['age'];
    return $posts;
}

// Search

if(isset($_POST['search']))
{
    $data = getPosts();
    
    $search_Query = "SELECT * FROM users WHERE id = $data[0]";
    
    $search_Result = mysqli_query($connect, $search_Query);
    
    if($search_Result)
    {
        if(mysqli_num_rows($search_Result))
        {
            while($row = mysqli_fetch_array($search_Result))
            {
                $id = $row['id'];
                $fname = $row['fname'];
                $lname = $row['lname'];
                $age = $row['age'];
            }
        }else{
            echo 'No Data For This Id';
        }
    }else{
        echo 'Result Error';
    }
}


// Insert
if(isset($_POST['insert']))
{
    $data = getPosts();
    $insert_Query = "INSERT INTO `users`(`fname`, `lname`, `age`) VALUES ('$data[1]','$data[2]',$data[3])";
    try{
        $insert_Result = mysqli_query($connect, $insert_Query);
        
        if($insert_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Inserted';
            }else{
                echo 'Data Not Inserted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Insert '.$ex->getMessage();
    }
}

// Delete
if(isset($_POST['delete']))
{
    $data = getPosts();
    $delete_Query = "DELETE FROM `users` WHERE `id` = $data[0]";
    try{
        $delete_Result = mysqli_query($connect, $delete_Query);
        
        if($delete_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Deleted';
            }else{
                echo 'Data Not Deleted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Delete '.$ex->getMessage();
    }
}

// Edit
if(isset($_POST['update']))
{
    $data = getPosts();
    $update_Query = "UPDATE `users` SET `fname`='$data[1]',`lname`='$data[2]',`age`=$data[3] WHERE `id` = $data[0]";
    try{
        $update_Result = mysqli_query($connect, $update_Query);
        
        if($update_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Updated';
            }else{
                echo 'Data Not Updated';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Update '.$ex->getMessage();
    }
}



?>


<!DOCTYPE Html>
<html>
    <head>
        <title>PHP INSERT UPDATE DELETE SEARCH</title>
    </head>
    <body>
        <form action="php_insert_update_delete_search.php" method="post">
            <input type="number" name="id" placeholder="Id" value="<?php echo $id;?>"><br><br>
            <input type="text" name="fname" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
            <input type="text" name="lname" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
            <input type="number" name="age" placeholder="Age" value="<?php echo $age;?>"><br><br>
            <div>
                <!-- Input For Add Values To Database-->
                <input type="submit" name="insert" value="Add">
                
                <!-- Input For Edit Values -->
                <input type="submit" name="update" value="Update">
                
                <!-- Input For Clear Values -->
                <input type="submit" name="delete" value="Delete">
                
                <!-- Input For Find Values With The given ID -->
                <input type="submit" name="search" value="Find">
            </div>
        </form>
    </body>
</html>


///////////////OUTPUT:  
php add edit remove find




                     

Share this

Related Posts

Previous
Next Post »

13 comments

comments
6 août 2016 à 10:06 delete

Thank You Very Much Bro !! :D

100% Worked !! :D

Reply
avatar
18 octobre 2016 à 07:44 delete

THANKS <3 i love you , thx

Reply
avatar
21 janvier 2017 à 19:15 delete

great big thanks, mind if i ask. do you have a tutorial like this where a user can upload profile picture?

Reply
avatar
16 mars 2017 à 01:36 delete

all php code is put in single page or different pages ?

Reply
avatar
18 mars 2017 à 03:09 delete

Great Code! I applied it to a meta project of mine and it worked like a charm. Instead of ID I put a Lot Number (unsigned int unique) as primary and it did the trick.

Now I have to make a few changes to the concept design of my database.

The Lot Number is not unique anymore. There might be more than one data with that same number. Is there a way to loop the search through all the found data, or show a dropdown named "ID" which will make it possible to navigate through all found data that match that same Lot Number?

Thank you in advance for your advice! Keep up with the great work!

Regards!

Reply
avatar
25 juillet 2017 à 03:04 delete

Good information
href=”https://indglobal.in/”>website design in bangalore
href=”https://indglobal.in/”>ecommerce website design in bangalore
href=”https://indglobal.in/”>mobile app development

Reply
avatar