This document discusses CRUD operations in PHP using OOP. It describes creating a customers table in a database, then creating a customers.php file that defines a Customers class. The class connects to the database and contains methods for inserting new customer data, and displaying all customer data by selecting records from the customers table.
This document discusses CRUD operations in PHP using OOP. It describes creating a customers table in a database, then creating a customers.php file that defines a Customers class. The class connects to the database and contains methods for inserting new customer data, and displaying all customer data by selecting records from the customers table.
This document discusses CRUD operations in PHP using OOP. It describes creating a customers table in a database, then creating a customers.php file that defines a Customers class. The class connects to the database and contains methods for inserting new customer data, and displaying all customer data by selecting records from the customers table.
This document discusses CRUD operations in PHP using OOP. It describes creating a customers table in a database, then creating a customers.php file that defines a Customers class. The class connects to the database and contains methods for inserting new customer data, and displaying all customer data by selecting records from the customers table.
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"; } } } ?>