Q:

PHP PDO Inserting data into tables

0

PHP PDO Inserting data into tables

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

PHP code to insert data into table using PDO

<?php
//Connection Variables
$host = "localhost";
$uname = "username";
$pw = "password";
$db = "DBtest";

try {
    $conn = new PDO("mysql:host=$host;dbname=$db", $uname, $pw);

    // set error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // SQL insert query
    $sql = "INSERT INTO users (firstname, lastname, email)
    VALUES ('John', 'Abraham', 'john@abraham.com')";

    // use exec() because no results are returned
    $conn->exec($sql);

    echo "New record created successfully";
}
catch(PDOException $e) {
    echo $sql . $e->getMessage();
}

//Set Connection state to null
$conn = null;

?>

Output

New record created successfully

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now