function that get fullname, username, password and email and insert a record in database in php

To insert a record into a MySQL database in PHP, you can use the following steps:

  1. Establish a connection to your database using mysqli_connect() or PDO.
  2. Build an INSERT INTO SQL statement with placeholders for your data.
  3. Prepare your SQL statement and bind your data to the placeholders.
  4. Execute the prepared statement.

Here's an example function that takes fullname, username, password, and email as parameters and inserts a record into a database:

main.php
function insertRecord($fullname, $username, $password, $email) {
    // establish a connection to your database
    $conn = mysqli_connect("localhost", "username", "password", "database_name");

    // build your SQL statement with placeholders  
    $sql = "INSERT INTO users (fullname, username, password, email) VALUES (?, ?, ?, ?)";

    // prepare your statement
    $stmt = mysqli_prepare($conn, $sql);

    // bind your data to the placeholders
    mysqli_stmt_bind_param($stmt, "ssss", $fullname, $username, $password, $email);

    // execute your statement
    mysqli_stmt_execute($stmt);

    // close your statement and connection
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
}
700 chars
21 lines

Note that this example uses the mysqli extension. If you prefer to use PDO, you can use the following code to prepare and execute your statement:

main.php
// prepare your statement
$stmt = $conn->prepare("INSERT INTO users (fullname, username, password, email) VALUES (?, ?, ?, ?)");

// bind your data to the placeholders
$stmt->bindParam(1, $fullname);
$stmt->bindParam(2, $username);
$stmt->bindParam(3, $password);
$stmt->bindParam(4, $email);

// execute your statement
$stmt->execute();
338 chars
12 lines

related categories

gistlibby LogSnag