How can creating a separate table for rental transactions improve data management and tracking in a PHP and MySQL setup?
Creating a separate table for rental transactions in a PHP and MySQL setup can improve data management and tracking by organizing all rental-related data in one place. This allows for easier querying and reporting on rental transactions, as well as simplifying the process of adding, updating, and deleting rental records.
// Create a separate table for rental transactions
$createTableQuery = "CREATE TABLE rental_transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
rental_date DATE,
customer_id INT,
movie_id INT,
rental_fee DECIMAL(10, 2)
)";
// Execute the query to create the table
mysqli_query($conn, $createTableQuery);
// Insert a rental transaction record into the new table
$insertQuery = "INSERT INTO rental_transactions (rental_date, customer_id, movie_id, rental_fee)
VALUES ('2022-01-01', 1, 5, 5.99)";
mysqli_query($conn, $insertQuery);
// Retrieve rental transactions from the new table
$selectQuery = "SELECT * FROM rental_transactions";
$result = mysqli_query($conn, $selectQuery);
// Loop through the results and display them
while ($row = mysqli_fetch_assoc($result)) {
echo "Rental ID: " . $row['id'] . " | Rental Date: " . $row['rental_date'] . " | Customer ID: " . $row['customer_id'] . " | Movie ID: " . $row['movie_id'] . " | Rental Fee: $" . $row['rental_fee'] . "<br>";
}
Keywords
Related Questions
- What steps can be taken to troubleshoot and resolve the "Failed to fetch" error when working with PHP and JS for JSON API integration?
- Is it possible to update MD5 hashed passwords to a more secure hash without the original plaintext password?
- How can the use of PHP functions like mysql_fetch_array() be optimized for better performance and error handling?