Before, My website can add data to the database, then suddenly it stopped from doing it, indicating an Error that is defined on my program, which means that it failed to add the data to the database, but the update and Delete works fine…
My SQL queries use PDO Statements
Here is the part of my code that adds the data to the db
function Addpost($post, $title, $sta, $category, $pid, $bg_data) {
$time_stum = date("h:i:s A , F j Y ");
$sort_date = date("Y n j H:i:s");
try {
$this->openConnection();
$sql = "INSERT INTO post (post_name, post, cat, role, post_id, date, bg_data)
VALUES (:b, :a, :d, :c, :e, :f, :h )";
$statement = $this->dbh->prepare($sql);
$result = $statement->execute(array(
':a' => $post,
':b' => $title,
':c' => $sta,
':d' => $category,
':e' => $pid,
':f' => $time_stum,
':h' => $bg_data
));
if($result == 1) {
return "Inserted";
} else {
echo "The data was not added";
}
$this->closeConnection();
}catch (Exception $e) {
$e->getMessage();
}
}
It works totaly fine even with latest php and sql version too, but when it comes the this hosting server, it just fails.
just tried it Right now, all it says was “The data was not added” as of the pre-defined message from the conditional statement, I work on getting an error message instead of that… but how exactly
Nope… adding “;” to the end of the query does not solve the Problem…
My solution was to change the non-working query into the generated sql query which is generated when you add an entry manually to the table, so far that’s what I found out. Maybe thats the solution for the problems about entry adding to the Database. Well this solution works with PDO statements, I don’t know with other types.
That’s because $result is a boolean. It’s TRUE on success and FALSE on failure. When you do echo $result, then the boolean $result is converted to a string, which becomes a blank string. So yes: if $result is false then echo $result shows nothing.
If you want to see why it breaks, you may be able to see so by dumping the result of $statement->errorInfo().
One other thing you show in both your query and connection scripts is this:
This is not very useful because you catch the exception but then don’t do anything with it. You call $e->getMessage() but that just returns the message. It’s up to you to do something with the message. If you want to see the error message, you need to echo it, log it or do something else with it.
With this code, your script will complete “successfully” and show no output if an exception is thrown. Which is actually worse than if you did not catch the exceptions at all (because then the general PHP error handler would handle it and you can see how your code crashed and why).