$name = 'John Smith'; $email = 'john@example.com'; $password = 'password'; $sql = "INSERT INTO users (name, email, password) VALUES (:name, :email, :password)"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':name', $name); $stmt->bindParam(':email', $email); $stmt->bindParam(':password', $password); $stmt->execute();
$products = [ ['name' => 'Product 1', 'price' => 10], ['name' => 'Product 2', 'price' => 20], ['name' => 'Product 3', 'price' => 30] ]; $stmt = $pdo->prepare("INSERT INTO products (name, price) VALUES (:name, :price)"); foreach ($products as $product) { $stmt->bindParam(':name', $product['name']); $stmt->bindParam(':price', $product['price']); $stmt->execute(); }The PDO library is used in both examples to connect to the database and execute the insert statement. It provides a secure way to insert data and prevents SQL injection attacks.