Adding Data to Database fails but the Update and Delete works fine


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 {
        $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";

    }catch (Exception $e) {

It works totaly fine even with latest php and sql version too, but when it comes the this hosting server, it just fails.

Can you show “openConnection()”? Remember to blank out your password. Also, can you give the database name as well?

protected $host ="******";
protected $username = "epiz_********";
protected $password = "**********";
protected $database_name = "epiz_24987541_gb";
protected $dbh = null;

public function openConnection() {
    try {
        $this->dbh = new PDO("mysql:host=". $this->host.";dbname=". $this->database_name,
            $this->username, $this->password);
    } catch (Exception $e) {

public function closeConnection() {
    try {
        $this->dbh = null;
    } catch (Exception $e) {

That is my open connection function, it works fine with UPDATE and Delete queries , only the INSERT seems being weird these days…

I am planning to overhaul the whole website, but this problem keeps me from doing it until a solution is made …

Errors? Enable PHP errors from the control panel.

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

I’m not a PHP expert (Just started learning it actually), but this looks like it might work here.

if($result == 1) {
            return "Inserted";
        } else {
            echo "The data was not added!";
			echo $result;

I tried to echo the result but it echoes nothing

Just Found a solution to it:

Here is the old query:

$sql = "INSERT INTO comments (cid, post_id, comment, date) 
 VALUES (:a, :b, :c, :d)";

Here is the new query that I added instead of that old one which is generated from the PHP myadmin querry upon adding an entry manually:

$sql = "INSERT INTO comments (id, cid, type, post_id, comment, ppic, pointed, date, reg_date) 
VALUES (NULL, :a , '', :b , :c , '', '0', :d , CURRENT_TIMESTAMP);";

and the script was able to add the DATA to the DB


My hypothesis is that : Maybe the sql db is much more strick this time???
or maybe the “;” is really needed at the end of the query

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.

Time to Deactivate the web and fix it… goodluck

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).


Thanks for the advice, though I found the solution… but this info would be useful in the future… thanks admin, your the best ありがとうございます。

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.