Mysqli INSERT anomaly

I have the following table:

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

I am trying to insert a row into a table. It runs without errors, but nothing is inserted into the 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 works fine and redirects to the success page, but nothing was found inside the table. Why doesn't it work?

+3
source share
4 answers

What about replacing DESCTIPTIONthe DESCRIPTIONinside $query?

Edit

Just out of curiosity, I created a table called mytableand copied your code into a PHP script.

Everything worked fine here, and the rows were inserted, except that the associated parameter was CURDATE()not executed properly, and the cell ENTRYDATEwas assigned 0000-00-00.

, , script?

error_reporting(E_ALL);?

, script ?

:

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();
}
+2

, ? , .

, PDO .

. PDO? , .

+2

:

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

$stmt->execute(). , "CUR_DATE()" (sic) DATE.

, execute() errorInfo():

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

Remember that an error execute()does not automatically throw an exception. You will need to check the success and failure for yourself.

+2
source

Is it possible that auto-commit is turned off?

If so, you should lock your insert like this

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

Hi

0
source

Source: https://habr.com/ru/post/1796008/


All Articles