I'm using PDO prepared statements to put things in my database (running-on localhost, using WAMP). This code worked perfectly for a project but now that I'm copying it over to another one it stopped working.
I added try/catch conditions but it never fails (there's never an exception) however the code isn't working properly.
pdoconf.php
<?php
$dsn = "mysql:host=localhost;dbname=webwiki;charset=utf8mb4"; // DSN
$options = [ // PDO options
PDO::ATTR_EMULATE_PREPARES => false, //
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
// Try/Catch if exception on creating PDO instance
try {
$pdo = new PDO($dsn, "root", "", $options); // Make new PDO instance
}
catch(PDOException $e) {
var_dump($e);
}
registerhandler.php
<?php
include("pdoconf.php");
var_dump($_POST);
try {
$statement = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
$statement->execute([$_POST["username"], $_POST["email-address"], $_POST["password"]]);
} catch(PDOException $e) {
var_dump($e->getMessage());
}
The behavior of the program: My register.php sends form data to my registerhandler.php script (the var_dump
shows all the data from the form). Then I want my script to take the data and put it in my database.
Expected output: The $_POST
data gets added to my database
Actual output: Nothing! Nothing gets added to my database, if I add an echo
after my catch
condition in registerhandler.php it doesn't get executed leading me to believe there is an error with $pdo->execute()
question from:
https://stackoverflow.com/questions/66055966/pdo-execute-doesnt-work-but-doesnt-return-en-exception-php 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…