// Connect to database $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); // Prepare the insert statement $stmt = $conn->prepare("INSERT INTO users (firstname, lastname, email) VALUES (?, ?, ?)"); // Bind the parameters $stmt->bind_param("sss", $firstname, $lastname, $email); // Set the values $firstname = 'John'; $lastname = 'Doe'; $email = 'johndoe@email.com'; // Execute the statement $stmt->execute();
// Connect to database $dbh = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password'); // Prepare the insert statement $stmt = $dbh->prepare("INSERT INTO users (firstname, lastname, email) VALUES (?, ?, ?)"); // Set the values $firstname = 'John'; $lastname = 'Doe'; $email = 'johndoe@email.com'; // Bind the parameters $stmt->bindParam(1, $firstname); $stmt->bindParam(2, $lastname); $stmt->bindParam(3, $email); // Execute the statement $stmt->execute();In this example, we connect to a MySQL database using the PDO package library, prepare an insert statement with placeholders (?), set the values for each parameter, bind the parameters to the statement using bindParam() method, and then execute the statement to add a new user record to the database. Both of the examples above use different package libraries, MYSQLi and PDO respectively. MYSQLi is a PHP extension for accessing databases while PDO is a PHP data-access abstraction layer that provides consistent access to various databases. The choice of package/library depends on the specific requirements of the project or application.