开发者

mysqli INSERT anomaly

开发者 https://www.devze.com 2023-02-14 08:47 出处:网络
I have the following table: ID: bigint autoinc NAME: varchar(255) DESCRIPTION: text ENTRYDATE: date I am trying to insert a row into the table. It executes without error but nothing gets inserted i

I have the following table:

ID: bigint autoinc
NAME: varchar(255)
DESCRIPTION: text
ENTRYDATE: date

I am trying to insert a row into the table. It executes without error but nothing gets inserted in database.

try {
    $query = "INSERT INTO mytable (NAME, DESCRIPTION, ENTRYDATE) VALUES(?,?,?)";
    $stmt = $conn->prepare($query);
    $name= 'something';
    $desc = 'something';
    $curdate = "CURDATE()";
    $stmt->bind_param("sss", $name, $desc, $curdate);
    $stmt->execute();
    $stmt->close();
    $conn->close();
    //redirect to success page
}
catch(Exception $e) {
    print $e;
}

It runs fine and redirects to success page but nothing can be found inside开发者_开发问答 the table. Why isn't it working?


What about replacing DESCTIPTION with DESCRIPTION inside the $query?

Edit

Just out of curiosity, I created a table called mytable and copy-pasted your code into a PHP script.

Here everything worked fine and rows got inserted, except that the binded parameter CURDATE() did not execute properly and the ENTRYDATE cell was assigned 0000-00-00.

Are you sure you are monitoring the same database and table your script is supposedly inserting to?

What happens when going with error_reporting(E_ALL); ?

Have you verified that the script actually completes the insertion?

The following appears to be working as expected:

error_reporting(E_ALL);

try {
    $query = "INSERT INTO mytable (NAME, DESCRIPTION, ENTRYDATE) VALUES (?, ?, CURDATE())";
    $stmt = $conn->prepare($query);
    $name= 'something';
    $desc = 'something';
    $stmt->bind_param("ss", $name, $desc);
    $stmt->execute();

     if ($conn->affected_rows < 1) {
        throw new Exception('Nothing was inserted!');
     }

    $stmt->close();
    $conn->close();
    //redirect to success page
}
catch(Exception $e) {
    print $e->getMessage();
}


Are you sure there is no error? There seems to be a typo in your column name for example.

Note that PDO is extremely secretive about errors by default.

See How to squeeze error message out of PDO? on how to fix this.


Try preparing this query instead:

"INSERT INTO mytable (NAME, DESCRIPTION, ENTRYDATE) VALUES(?,?,CUR_DATE())"

And check the results of $stmt->execute(). It would have given you a warning that "CUR_DATE()" (sic) is not a valid DATE.

You can check if a statement was correctly executed by checking the return value of execute() and querying the errorInfo() method:

if (!$stmt->execute()) {
   throw new Exception($stmt->errorInfo(), stmt->errorCode());
}

Be aware that upon failure, execute() does not throw an exception automagically. You'll have to check for successful operation and failure for yourself.


Is it possible that autocommit is OFF?

If so then you have to commit your insert like so

/* commit transaction */
$conn->commit();

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消