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
<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);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
}
?>
Index.php
<?php
session_start();
?>
<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
session_start();
include 'classes.php';
$insert=new basic_operation();
$usr=$insert->Insert_Data();
$_SESSION['insert']='data inserted successfuly';
header('location:index.php');
?>
Classes.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();
}
}
}
?>