MODUL 5.1 CRUD DGN PHP OOP

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

MODUL 5.

CRUD dengan PHP OOP

1. Membuat Tabel customers

2. Membuat Halaman Web


a) Membuat file customers.php
<?php
class Customers
{
private $servername = "localhost";
private $username = "root";
private $password = "";
private $database = "crud_oop";
private $con;

public function __construct()


{
$this->con = new mysqli ($this->servername, $this->username,
$this->password, $this->database);
if (mysqli_connect_error()){
trigger_error("Failed to connect to MySQL : ".
mysqli_connect_error());
}else{
return $this->con;
}
}

public function insertData($post)


{
$name = $this->con->real_escape_string
($_POST['name']);
$email = $this->con->real_escape_string
($_POST['email']);
$username = $this->con->real_escape_string
($_POST['username']);
$password = $this->con->real_escape_string
(md5($_POST['passsword']));
$query = "INSERT INTO customers (name,email,username,password)
VALUES ('$name', '$email', '$username', '$password')";
$sql = $this->con->query($query);
if ($sql == true){
header ("Location:index.php?msg1=insert");
}else{
echo "Registration failed try again!";
}
}
public function displayData()
{
$query = "SELECT * FROM customers";
$result = $this->con->query($query);
if ($result->num_rows > 1) {
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}else{
echo "No Found records";
}
}
}
?>

You might also like