i need you to write php code for delivery pricing admin page whereby the seller can add, edit or delete shipping locations and prices, the table looks like this in the database:
id INT AUTO_INCREMENT PRIMARY KEY,
area VARCHAR(255) NOT NULL,
county VARCHAR(255) NOT NULL,
category ENUM('pickup mtaani', 'doorstep', 'by bus','pick in shop') NOT NULL,
price DECIMAL(10, 2) NOT NULL
this is for an admin page so all admin features are expected, youll create the html for it later just start with the php code for now
Assistant
Certainly! Below is a comprehensive PHP script for an Admin Delivery Pricing page that allows sellers to Add, Edit, and Delete shipping locations and prices. This script uses PDO for database interactions, which provides a secure and efficient way to handle database operations with prepared statements to prevent SQL injection.
Assumptions:
Database Connection: Replace the placeholder values with your actual database credentials.
Authentication: It's assumed that the admin authentication is handled elsewhere.
HTML Forms: Placeholder comments are added where HTML forms and tables should be integrated.
Starts a session to handle flash messages like success notifications.
Database Connection:
Uses PDO to connect to the MySQL database.
Replace 'your_database', 'your_username', and 'your_password' with your actual database credentials.
Error and Success Handling:
Initializes arrays to store errors and success messages.
Uses $_SESSION to store success messages and displays them after a redirect to prevent form resubmission issues.
Add Operation:
Checks if the request method is POST and the action is 'add'.
Retrieves and sanitizes input data from the form.
Validates the input data before inserting it into the database.
Uses a prepared statement to securely insert data into the delivery_pricing table.
Redirects back to the admin page after successful insertion to prevent duplicate submissions.
Edit Operation:
Similar to the Add operation but updates an existing record based on the provided id.
Ensures the id is valid before attempting to update.
Delete Operation:
Checks if the request method is POST and the action is 'delete'.
Retrieves the id of the record to be deleted.
Uses a prepared statement to securely delete the record from the database.
Includes a JavaScript confirm dialog in the HTML placeholder to prevent accidental deletions.
Fetching Data:
Retrieves all existing shipping locations from the delivery_pricing table to display them in a table.
HTML Placeholder:
Provided within PHP comments. You should replace this section with your actual HTML, integrating forms for adding, editing, and deleting shipping locations.
The example HTML includes:
A form to add new shipping locations.
A table displaying existing shipping locations with forms embedded for editing and deleting each entry.
Final Notes:
This script provides a solid foundation for managing delivery pricing through an admin interface. Ensure that you handle user authentication and authorization to restrict access to this admin page only to authorized personnel. Additionally, always keep security best practices in mind when dealing with administrative operations.