Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

PDO Execute doesn't work but doesn't return en exception | PHP

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...