PHP and MySQL

PHP INSERT DATA IN TO MYSQL

0  0 2016-09-03 07:02:13

Database and tables are created in mysql server; then after that we add the data in database in tables.
Using following code provided by PHP and SQL with special syntax, we can add the data:-
The SQL query we should use in PHP code
String values must be included in query must be quoted.
Numeric values can be without quoted
The word NULL has to be included in quotes.

The “INSERT INTO” statement is used to add new values to a MySQL table:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

We created an empty table named  as "My_Guests" with 5 columns:
"id", "fname", "name", "emailid" and "bdate".
If we use AUTO_INCREMENT for column (like the "id" column) or TIMESTAMP (like the "reg_date" column), there is no need to be specified in the SQL query;
MySQL will automatically do AUTO_INCREMENT the provided id as primary key.

The below example will add a new record to the "My_Guests" table:
Example (MySQL Object-oriented with php code)
<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "My_Guests";

    // Create connection using php to mysql database
    $conn  = mysql($servername, $username, $password, $dbname);
    // Check connection using php to mysql database
    if (!$conn)
    {
        die("Connection failed: " . $conn);
    }

    $query = "INSERT INTO My_Guests (fname, lname, emailid)
    VALUES ('Nikhil', 'Madal', 'nikhil.madal@aquaspade.com')";

    if ($conn($sql) === TRUE)
    {
        echo “Record created successfully";
    } else {
        echo "Error: " . $query . "<br>" . $conn;
               }

$conn->close();
?>

Example (MySQL Procedural)
<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "My_Guests";

    // Create connection using php to mysql database
    $conn = mysql_connect($servername, $username, $password, $dbname);
    // Check connection using php to mysql database
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());}

    $query = "INSERT INTO My_Guests (fname, lname, emailid)
    VALUES ('Nikhil', 'Madal', 'nikhil.madal@aquaspade.com')";

    if (mysql_query($conn, $query))
    {
        echo "Record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysql_error($conn);}

    mysql_close($conn);
?>

Example (PDO)
<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "My_Guets";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception using php to mysql database
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO My_Guests (firstname, lastname, email)
        VALUES ('Nikhil', 'Madal', 'nikhil.madal@aquaspade.com')";
        // use exec() because no results are returned using php to mysql database
        $conn->exec($sql);
        echo "New record created successfully";
    }
    catch(PDOException $e)
    {
        echo $sql . "<br>" . $e->getMessage();
    }
    $conn = null;
?>

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Ziuby
Ziuby