[英]Inserting values from one table to another table sql and php
I'm currently doing a Book Rental System using PHP with a bit of OOP.我目前正在使用 PHP 和一点 OOP 做一个图书出租系统。 I'm trying to insert the information from table book (id, bookTitle) and table users (user_id, fullname) to table borrowbook but I don't know how.我正在尝试将表书(id,bookTitle)和表用户(user_id,全名)中的信息插入表借书,但我不知道如何。 I'm just a beginner in PHP我只是 PHP 的初学者
TABLE BOOK
id | bookId | bookTitle | authorName
TABLE USERS
user_id | uname | upass | fullname | uemail
TABLE BORROWBOOK
borrow_id | borrowDate | returnDate | b_Title | u_Borrower | status | b_id | u_id
u_id and b_id are foreign keys of user_id and id u_id 和 b_id 是 user_id 和 id 的外键
Here's the output这是 output
Here's the code这是代码
<?php
class Borrow {
public $bookId;
public $bookTitle;
public $bookAuthor;
public $id;
public $a_id;
public $authorId;
public $fName;
public $lName;
public $borrow_id;
public $borrowDate;
public $returnDate;
public $b_Title;
public $u_Borrower;
public $status;
private $conn;
public function __construct($db) {
$this->conn = $db;
}
public function displayAll() {
$sql = "SELECT * FROM book";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
return $stmt;
}
public function displayRequest() {
$sql = "SELECT * FROM borrowbook";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
return $stmt;
}
public function selectBook() {
$sql = "SELECT *
FROM book as A
LEFT JOIN borrowbook AS BK ON A.bookId = BK.b_id
LEFT JOIN users AS U ON U.user_id = BK.u_id
WHERE id = ?";
//$sql = "SELECT * FROM book WHERE id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindparam(1, $this->id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->id = $row['id'];
$this->bookId = $row['bookId'];
$this->bookTitle = $row['bookTitle'];
$this->authorName = $row['authorName'];
}
public function insertBook() {
$sql = "INSERT INTO borrowbook SET borrowDate = ?, returnDate = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindparam(1, $this->borrowDate);
$stmt->bindparam(2, $this->returnDate);
$stmt->execute();
}
public function insertInfo() {
$sql = "INSERT INTO borrowbook (u_Borrower, b_Title, b_id, u_id)
SELECT u.fullname, b.bookTitle, b.id, u.user_id
FROM users AS u, book AS b
WHERE u.user_id = b.id WHERE id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindparam(1, $this->id);
$stmt->execute();
}
public function readAll($from_record_num, $records_per_page) {
$sql = "SELECT * FROM book ORDER BY bookId ASC LIMIT {$from_record_num}, {$records_per_page}";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
return $stmt;
}
public function countAll() {
$sql = "SELECT id FROM book";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$num = $stmt->rowCount();
return $num;
}
}
?>
<!-- INSERT INTO `bookborrow`(`b_id`, `b_Title`, `u_Borrower`) VALUES ((SELECT id FROM book WHERE id = 1),(SELECT bookTitle FROM book WHERE id = 1), (SELECT authorName FROM book WHERE id = 1)); -->
<!-- INSERT INTO `borrowbook`(`borrowDate`, `returnDate`, `b_Title`, `u_Borrower`, `status`) VALUES('June 10', 'June 13', (SELECT bookTitle FROM book WHERE id = 1), (SELECT fullname FROM users WHERE user_id = 5), 1) -->
A few things:一些东西:
The borrow table connects the book and user table, so you need the id from each table:借用表连接 book 和 user 表,因此您需要每个表的 id:
Insert borrowed book:插入借来的书:
public function insertInfo($user_id, $book_id) {
$sql = "INSERT INTO borrowbook (u_Borrower, b_Title, b_id, u_id)
SELECT u.fullname, b.bookTitle, b.id, u.user_id
FROM users AS u, book AS b
WHERE u.user_id = {$user_id} AND b.id = {$book_id}";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
}
When updating a row, use UPDATE, not INSERT.更新行时,使用 UPDATE,而不是 INSERT。 And be sure to include the where clause or every row will be updated.并确保包含 where 子句,否则每一行都会被更新。
Update book:更新书:
public function updateBook($user_id, $book_id) {
$sql = "UPDATE borrowbook SET borrowDate = ?, returnDate = ? WHERE u_id = {$user_id} AND b_id = {$book_id}";
$stmt = $this->conn->prepare($sql);
$stmt->bindparam(1, $this->borrowDate);
$stmt->bindparam(2, $this->returnDate);
$stmt->execute();
}
As @Barmar mentioned, when joining tables, you only want to store the IDs and new information.正如@Barmar 提到的,在加入表时,您只想存储 ID 和新信息。 Don't save the book title because it is already stored in the book table.不要保存书名,因为它已经存储在书表中。
The preferred structure of the borrow table is this:借用表的首选结构是这样的:
TABLE BORROWBOOK
borrow_id | borrowDate | returnDate | status | b_id | u_id
If you move the status to the book table, you can update the status using this method:如果将状态移动到书表,则可以使用以下方法更新状态:
public function setBookStatus() {
$sql = "UPDATE book SET status = ? WHERE id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindparam(1, $this->status);
$stmt->bindparam(2, $this->b_id);
$stmt->execute();
}
In the page, you need to store the book id (b_id) for the update.在页面中,您需要存储图书 id (b_id) 以进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.