Click here to Skip to main content
15,916,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I am new to PHP and trying to learn OPP PHP.

i am creating a registration form (a simple one) to learn OPP PHP, when i submit data the success message shows up but data is not inserted in data base.




If any one knows how to perform basic CRUD (insert, update,delete,select) operation in PHP using OPP PHP and PDO Then please provide the link, source, Example, so i can learn from it

I searched on the internet but bearly able to find the connection code... Looking for a descriptive answer, as i think this question will also help a lot of people who try/want to learn OOP PHP.

Any help will appreciated.

Thanks.

What I have tried:

connection.php

PHP
<pre lang="C#"><?php

class DBConnection extends PDO
{
    public function __construct()
    {
        $host='mysql:host=localhost;dbname=OOP';
        $user='root';
        $password='';
        parent::__construct($host,$user,$password);
        $this->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // always disable emulated prepared statement when using the MySQL driver
        $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }
}

?>



Index.php

PHP
<?php
session_start();
/*include 'classes.php';
//$dbHandle = new DBConnection();
$ins=new basic_operation();
$ins->Insert_Data();*/



?>


    <div class="col-lg-12">

        <div class="col-lg-1"></div>

        <div class="col-lg-7">
            <form action="user_data.php" method="post" >
                <input type="text" name="username" id="username" class="form-control"><br>
                <input type="email" name="email" id="email" class="form-control"><br>
                <input type="password" name="password" class="form-control" id="password"><br>
                <input type="submit" name="submit" id="submit" value="submit">
            </form>
                    </div>

                </div>
<div>
    <?php if (isset($_SESSION['insert']))
    {
        echo $_SESSION['insert'];
        unset($_SESSION['insert']);
    }

    ?></div>

        </div>
    </div>


User_data.php

PHP
<?php
session_start();
include 'classes.php';

    $insert=new basic_operation();
    $usr=$insert->Insert_Data();
    //return $insert;
    $_SESSION['insert']='data inserted successfuly';
    header('location:index.php');

?>


Classes.php

PHP
<?php
include 'connection.php';

class basic_operation
{

    public function Insert_Data()
    {
        if (isset($_POST['submit'])) {
            $user = $_POST['username'];
            $email = $_POST['email'];
            $pass = $_POST['password'];
            $smt = new DBConnection();
            $qry = $smt->prepare("insert into student(User_Name,Email,Password) VALUES ('" . $user . "','" . $email . "','" . $pass . "')");
            $qry->execute();

        }
    }
}
?>
Posted
Updated 20-May-16 22:11pm

1 solution

Check this out tip[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900