view plaincopy to clipboardprint
- PHP Data Objects (PDO) Step By Step Tutorial - Part 9: In this post, we will implement prepared statement for insert and update data. I will show simple example.
- <?php
-
- $dbtype = "sqlite";
- $dbhost = "localhost";
- $dbname = "test";
- $dbuser = "root";
- $dbpass = "admin";
-
- $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
-
- $title = 'PHP Security';
- $author = 'Jack Hijack';
-
- $sql = "INSERT INTO books (title,author) VALUES (:title,:author)";
- $q = $conn->prepare($sql);
- $q->execute(array(':author'=>$author,
- ':title'=>$title));
- ?>
- Example for update data:
- <?php
-
- $dbtype = "sqlite";
- $dbhost = "localhost";
- $dbname = "test";
- $dbuser = "root";
- $dbpass = "admin";
-
- $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
-
- $title = 'PHP Pattern';
- $author = 'Imanda';
- $id = 3;
-
- $sql = "UPDATE books
- SET title=?, author=?
- WHERE id=?";
- $q = $conn->prepare($sql);
- $q->execute(array($title,$author,$id));
- ?>
|