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




                     




21 commentaires:

  1. Thank You Very Much Bro !! :D

    100% Worked !! :D

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

    RépondreSupprimer
  3. 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!

    RépondreSupprimer
  4. What code should i use if i want the value of an option field? because the echo doesn't work with the option field...help me please :c

    RépondreSupprimer
  5. Thank you bro !!! thank u very much

    RépondreSupprimer
  6. hello
    in the action of my form, where is this file : php_insert_update_delete_search.php
    when i delete action in the form it don't work well

    RépondreSupprimer
  7. Your post is good and I like how you use audience engagement. I always like to give feedback if I can and your engagement rates are quite nice. I always try my best to be the first to comment just so I can get better engagement. .

    RépondreSupprimer
  8. I was glad to reveal this incredible site. I have to thank you for your time because of this incredible read!! I unquestionably delighted in all of it and I have you bookmarked to see new data on your blog .

    RépondreSupprimer