postheadericon PHP insert update, delete using PDO part-3


Part-3: Insert record using prepared statement in PDO.


In part three, I am creating two file one is the HTML file (insert.html) to define form to get the information and second one (insert.php) is the code behind file to retrieve the values from HTML and insert those values in database using prepared statement with the help of PDO.


insert.html


<!DOCTYPE html>
<html>
<body>
<form method="GET" action="insert.php">
Name  :<input type="text" name="txtName" required><br/>
Email :<input type="email" name="txtEmail" required><br/>
Phone :<input type="number" name="txtPhone" required><br/>
<input type="submit" value="Insert"><input type="reset" value="Clear"><br/>
</form>
</body>
</html>




insert.php


<?php
require("conn.php");
try
{
$tbl="tbl";

$name = $_GET["txtName"];
$email = $_GET["txtEmail"];
$phone = $_GET["txtPhone"];


/*
// prepare sql and bind parameters

$stmt = $conn->prepare("INSERT INTO $tbl (name, email, phone)
VALUES (:name, :email, :phone, :message)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':phone', $phone);
$stmt->execute();
*/


$stmt = $conn->prepare("INSERT INTO $tbl (name, email, phone) VALUES (?, ?, ?)");
$stmt->execute(array($name,$email,$phone));


$last_id = $conn->lastInsertId();

echo "<br>  Record [ $last_id ] inserted" ;
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>



This example is divided in multiple part.


Part-1: Configure and Setup the MySQL database connection using PDO.
Part-2: Create a MySQL Database and a table using PDO.
Part-3: Insert record using prepared statement in PDO.
Part-4: Display Records using prepared statement.
Part-5: Search records using AJAX and PDO in PHP.
Part-6: Update Record using prepared statement in PDO.
Part-7: Delete Record using prepared statement in PDO.



1 comments:

Tech IT Solutions said...

Your explanation is so easy and interesting. The person information about ins It is a wonderful post with full of information. I enjoyed reading each and everything in this post. Thank you very much for sharing this excellent post.

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.