Sunday 10 August 2014

How to insert data using pdo in three steps in php


Follow the Steps

Step1:Make a connection.
Step2: Pass the query into prepare() fucntion.
Step3: Execute the prepare statement.
Note: we already created Database as registration  and table as user
 Index.php
 <?php
$hostname='localhost';
$username='root';
$password='';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=registration",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to Database<br/>';
$name="enjoymyphp";
$password="12352";
$email="enjoymyphp@gmail.com";
$sql =$dbh->prepare("insert into user SET name='".$name."',password='".$password."',email='".$email."'");
$sql->execute();
}
catch(PDOException $e)
{

echo $e->getMessage();
}
?>


1 comment:

  1. My code is running successfully thanks. Keep it up buddy..

    ReplyDelete