// Connect to database $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); // SQL query to insert new order $sql = "INSERT INTO orders (customer_name, order_date, total_price) VALUES ('John Doe', '2021-10-01', 50.00)"; if ($conn->query($sql) === TRUE) { echo "Order saved successfully!"; } else { echo "Error saving order: " . $conn->error; } // Close database connection $conn->close();
// Connect to database using PDO $dsn = "mysql:host=localhost;dbname=database"; $username = "username"; $password = "password"; try { $conn = new PDO($dsn, $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Error connecting to database: " . $e->getMessage(); } // SQL query to insert new order $sql = "INSERT INTO orders (customer_name, order_date, total_price) VALUES ('Jane Doe', '2021-09-15', 75.00)"; try { $conn->exec($sql); echo "Order saved successfully!"; } catch (PDOException $e) { echo "Error saving order: " . $e->getMessage(); } // Close database connection $conn = null;This example uses the PHP Data Objects (PDO) extension to connect to a MySQL database and insert a new order into the "orders" table. It also includes the customer's name, order date, and total price. If the query is successful, it outputs a success message. If there is an error, it outputs the error message. Package/library used: PDO (built-in PHP extension for various database systems)