Answer the question
In order to leave comments, you need to log in
INSERT PDO how?
I will say right away, yesterday I started learning PDO on this question, probably not very ..... but still.
I write registration, but INSERT does not work at all.
require_once 'connect_db.php';
function createUser($login, $pass, $email){
global $pdo;
$sql = "INSERT INTO user (login, pass, email) VALUES (:login, :pass, :email)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':login', $login, PDO::PARAM_STR);
$stmt->bindParam(':pass', $pass, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->execute();
return true;
}
$create = createUser($loginReg, $passReg, $emailReg);
require_once 'connect_db.php';
$host = '127.0.0.1';
$db = 'messenger';
$user = 'root';
$pass = 'root';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
Answer the question
In order to leave comments, you need to log in
The code itself looks normal. the problem is most likely something else.
But no one knows what.
Only you can find out by looking at the error that PHP produces.
If the record is not added to the database, then an error has occurred.
If an error occurs, then you need to read its text and then correct it.
To see the text of the error, you must either look at the web server log, or enable error output on the screen and repeat the insert.
to understand what the problem is, one must read and understand what the error message says. You can also use Google translator and Google itself, if you still don’t understand what the error is or how to fix it.
You can see different options for error output settings here .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question