PDO Error: "SQLSTATE[HY000]: General Error" When Updating Database
Despite your code successfully updating the database, you encounter an enigmatic error upon execution: "SQLSTATE[HY000]: General error."
Inspecting your code, we notice an unexpected inclusion:
<code class="php">$result = $stmt->fetchAll();</code>
This line of code is typically used to retrieve results from select statements, but in the context of an update query, it's incorrect. Specifically, fetchAll() should not be used for insert or update queries. Its removal should resolve the error.
Thus, your updated code would be:
<code class="php">try { $stmt = $pdo->prepare("UPDATE $page SET $section = :new_content WHERE $section = '$old_content'"); $stmt->execute(array( 'new_content' => $new_content )); echo "Database updated!"; } catch(PDOException $e) { echo 'ERROR UPDATING CONTENT: ' . $e->getMessage(); }</code>
The above is the detailed content of Why Does PDO Show \'SQLSTATE[HY000]: General Error\' for Database Updates?. For more information, please follow other related articles on the PHP Chinese website!