PHP OOPS - Chèn dữ liệu trong cơ sở dữ liệu trong PHP OOP với hướng đối tượng MySQLI.
Trong bài đăng này, bạn sẽ tìm hiểu về cách chèn dữ liệu vào cơ sở dữ liệu trong PHP MySQL bằng khái niệm OOPS. Vì vậy, chúng tôi sẽ sử dụng Bootstrap 5 để thiết kế giao diện người dùng biểu mẫu HTML.
Chúng tôi sẽ đến đối tượng MySQLI được định hướng để chèn dữ liệu trong PHP OOP.MySqli Object Oriented to insert data in php oop.
Bước 1: Tạo bảng vào cơ sở dữ liệu của bạn như sau: Create a table into your database as follows:
Createdetableifnotexists`students` [ TABLE IF NOT EXISTS `students` [
`id` in [11] notnull, int[11] NOT NULL,
`fullname`varchar [191] notnull, varchar[191] NOT NULL,
`Email`varchar [191] notnull, varchar[191] NOT NULL,
`điện thoại`varchar [20] notnull, varchar[20] NOT NULL,
`khóa học`varchar [20] notnull, varchar[20] NOT NULL,
] Động cơ = innodb auto_increment = 3Default Charset = Latin1;3 DEFAULT CHARSET=latin1;
Bước 2: & nbsp; Tạo tệp & nbsp; file-add.php & nbsp; tệp và dán biểu mẫu HTML dưới đây được thiết kế bằng bootstrap. Create a student-add.php file and paste the below html form designed using bootstrap.
$this->conn = $conn;
{
}
}>
?> Create a code.php file and paste the below:
Học sinh thêm
= [
Học sinh thêm => mysqli_real_escape_string[$db->conn,$_POST['fullname']],
Họ và tên => mysqli_real_escape_string[$db->conn,$_POST['email']],
ID email => mysqli_real_escape_string[$db->conn,$_POST['phone']],
Khóa học => mysqli_real_escape_string[$db->conn,$_POST['course']],
Không có điện thoại
Lưu học sinh = new StudentController;
Bước 3: Tạo kết nối cơ sở dữ liệu. Tạo tệp dbconn.php và dán mã dưới đây: = $student->create[$inputData];
if[$result][$result]
PHP OOPS - Chèn dữ liệu vào cơ sở dữ liệu trong PHP MySQL bằng OOPS
{['message'] = "Student Added Successfully";
}["Location: student-add.php"];
exit[0];[0];
{
}
PHP OOPS - Chèn dữ liệu vào cơ sở dữ liệu trong PHP MySQL bằng OOPS
{['message'] = "Not Inserted";
}["Location: student-add.php"];
exit[0];[0];
{
}
}>
?> Create a StudentController.php file and paste the below to insert data into database.
Học sinh thêm
= $inputData['phone'];
Học sinh thêm = $inputData['course'];
Họ và tên = "INSERTINTO students [fullname,email,phone,course] VALUES ['$fullname','$email','$phone','$course']";
ID email = $this->conn->query[$studentQuery];
if[$result]{[$result]{
returntrue; true;
Khóa họcelse{
returnfalse; false;
{
{
}
}>
?>