V
V
Vlad Babin2018-02-04 22:58:06
PHP
Vlad Babin, 2018-02-04 22:58:06

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;
  }

I call this function in the form handler
$create = createUser($loginReg, $passReg, $emailReg);

file : its code is: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);

the connection is correct because SELECTs work ,
but I can’t add a new user. I
saw that they almost always create a database connection object, but I saw that it’s possible anyway.
What is the problem here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2018-02-05
@dudoser

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 question

Ask a Question

731 491 924 answers to any question