I have setting the code and query with database. I think everything is correct with PDO Connection to MySQL but the record did not go to the database.
// Includes start // Connection
<?php
$DSN = 'mysql:host = localhost; dbname = cms4.2.1';
$ConnectingDB = new PDO($DSN, 'root', '');
?>
// Redirect function
<?php
function Redirect_to($New_Location) {
header("Location:".$New_Location);
exit;
}
?>
// Errors handling
<?php
session_start();
function ErrorMessage(){
if (isset($_SESSION["ErrorMessage"])){
$Output = "<div class=\"alert alert-danger\">";
$Output .= htmlentities($_SESSION["ErrorMessage"]);
$Output .= "</div>";
$_SESSION["ErrorMessage"] = null;
return $Output;
}
}
function SuccessMessage() {
if (isset($_SESSION["SuccessMessage"])) {
$Output = "<div class=\"alert alert-success\">";
$Output .= htmlentities($_SESSION["SuccessMessage"]);
$Output .= "</div>";
$_SESSION["SuccessMessage"] = null;
return $Output;
}
}
?>
// Includes End
// The code
<?php require_once("Includes/DB.php"); ?>
<?php require_once("Includes/Functions.php"); ?>
<?php require_once("Includes/Sessions.php"); ?>
<?php
if (isset($_POST["Submit"])) {
$Category = $_POST["CategoryTitle"];
$Admin = "Abdullah";
date_default_timezone_set("Asia/Riyadh");
$CurrentTime = time();
$DateTime = strftime("%B-%d-%Y %H:%M:%S", $CurrentTime);
if (empty($Category)) {
$_SESSION["ErrorMessage"] = "All fields must be filled out";
Redirect_to("Categories.php");
} else if (strlen($Category) < 3) {
$_SESSION["ErrorMessage"] = "Category is short";
Redirect_to("Categories.php");
} else if (strlen($Category) > 49) {
$_SESSION["ErrorMessage"] = "Category is too long";
Redirect_to("Categories.php");
} else {
// Insert to database
// The problem starts here I think
global $ConnectingDB;
$sql = "INSERT INTO category(title,author,datetime)";
$sql .= "VALUES(:categoryName,:adminName,:dateTime)";
$stmt = $ConnectingDB->prepare($sql);
$stmt -> bindValue(':categoryName',$Category);
$stmt -> bindValue(':adminName',$Admin);
$stmt -> bindValue(':dateTime',$DateTime);
$Execute = $stmt -> execute();
if ($Execute) {
$_SESSION["SuccessMessage"] = "Category Added Successfully";
Redirect_to("Categories.php");
} else {
$_SESSION["ErrorMessage"] = "Something went wrong";
Redirect_to("Categories.php");
}
}
}
?>
The execute message always keeps "Something went wrong"
When I go to normal MySQL query system everything is ok. But I want to complete my work on PDO.
I think you can use pdoexception
then you will be able to print your errors and exceptions.
try{
$stmt = $ConnectingDB->prepare($sql);
$stmt -> bindValue(':categoryName',$Category);
$stmt -> bindValue(':adminName',$Admin);
$stmt -> bindValue(':dateTime',$DateTime);
$Execute = $stmt -> execute();
}catch( PDOException $e ) {
print $e->getMessage();
}
pdo should i try/catch every query?
Hope that help you.
Try inserting space before VALUES or after the closing bracket of the first row just like I did below, sometimes the query doesn't work because of bad written syntax.
$sql = "INSERT INTO category(title,author,datetime) ";
OR
$sql .= " VALUES(:categoryName,:adminName,:dateTime)";
This might not be the problem but it's good practice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.