I’m converting a website that build using MySQLi to PDO.
I search everywhere but I couldn’t find a good example. I just need the proper way of executing a inset query.
Database file
$host_name = "localhost";
$db_username = "root";
$db_password = "mysql";
$db_name = "mydatabase";
try {
$conn = new PDO("mysql:host=$host_name;dbname=$db_name", $db_username, $db_password);
$conn->exec("set names utf8");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
Submit file (witch take data from a user submitted form
if($_POST)
{
$post_title = $_POST['post'];
$post_cat = $_POST['post_cat'];
$stmt = $conn->prepare("INSERT INTO posts post_title, post_cat)
VALUES (:p_title, :p_cat)");
$stmt->bindParam(':p_title', $post_title);
$stmt->bindParam(':p_cat', $post_cat);
$stmt->execute();
$conn = null;
}
This insert the data in to the database without any issue. But how can check whether the rows get inserted or not. And display message "posted" or if there is a error "Failed"