Below is a sample code:

$k = $pdo->query("INSERT INTO kategories SET user_id=$user_id, in_ex=?, name=?"); $k->execute(array($_POST['in_ex'], $_POST['katName'])); 

When do I replace? on 'pyfxtybz', then the script works. Why then with question marks does not work? Mistake:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [42000]: Syntax error or access violation: 1064 You can have an error in your SQL syntax; check the syntax to use mySQL server; at line 1 'in C: \ web \ OpenServer \ domains \ test.loc \ tools.php on line 13

    1 answer 1

    PDO :: query () executes the SQL query without preparation and returns the result set (if any) as a PDOStatement object.

    You are trying through the query () method to use the query for the prepare () method.

    In addition, you have the wrong INSERT request - a mixture of INSERT and UPDATE

    Your code should be like this:

     $k = $pdo->query("INSERT INTO kategories (user_id, in_ex, name) VALUES (?,?,?)"); $k->execute(array($user_id, $_POST['in_ex'], $_POST['katName'])); 
    • Similarly, prepare needed to be used instead of query. - EduardRST
    • Thanks helped very much - EduardRST