1

I am migrating from normal SQL to PDO because I let a friend of mine test if I have any weak points and he adviced me to PDO because he found a lot of weak points.

So here is my full error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' in /home/ubuntu/workspace/post.php on line 54

( ! ) PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? (id, title, info_bys, info_shorts, info_longs, email, ' at line 1 in /home/ubuntu/workspace/post.php on line 54

And here is my code:

    $stmt = $db->prepare("INSERT INTO  :portal
    (`id`, `title`, `info_bys`, `info_shorts`, `info_longs`, `email`, `filename`, `filepath`, `filename2`, `filepath2`, `approved`) 
    VALUES ('', ':title', ':by_information', ':short', ':long_information', ':email', ':filename', ':filetarget', ':filename2', ':filetarget2',  'false'");
    $stmt->execute(array(':portal' => $portal, ':title' => $title, ':by_information' => $by_information, ':short' => $short, ':long_information' => $long_information, ':email' => $email, ':filename' => $fileName, ':filetarget' => $fileTarget, ':filename2' => $fileName2, ':filetarget2' => $fileTarget ));
    echo $affected_rows.' were affected';

Is there something I cant use in PDO that I can use in SQL or am I just typing the wrong stuff.

Hope someone can help.

EDIT:

New code:

    function buildQuery( $get_var ) 
{
    switch($get_var)
    {
        case 1:
        $portal = $_POST['portal'];
            break;
    }

                $stmt = $db->prepare("INSERT INTO  :portal
            (`id`, `title`, `info_bys`, `info_shorts`, `info_longs`, `email`, `filename`, `filepath`, `filename2`, `filepath2`, `approved`) 
            VALUES (:title, :by_information, :short, :long_information, :email, :filename, :filetarget, :filename2, :filetarget2,  'false'");
            $stmt->execute(array(':portal' => $portal, ':title' => $title, ':by_information' => $by_information, ':short' => $short, ':long_information' => $long_information, ':email' => $email, ':filename' => $fileName, ':filetarget' => $fileTarget, ':filename2' => $fileName2, ':filetarget2' => $fileTarget ));
            echo $affected_rows.' were affected';
}
Community
  • 1
  • 1
Rik Nijdeken
  • 50
  • 1
  • 10

1 Answers1

2

Three issues with the code:

  1. As stated by crhis85 (upvote), you can't bind table names.
  2. PDO prepare takes care of the escaping and quotes.

    VALUES ('', ':title', ':by_information', ':short', ':long_information', ':email', ':filename', ':filetarget', ':filename2', ':filetarget2', 'false'");

The issue here is that if you define a param to a string (PDO::PARAM_STR) the values are double quoted with single quotes. Instead do this:

`VALUES ('', :title, :by_information, :short, ....");`
  1. Don't insert an ID, this should be set on auto increment and is done automatically.

    'INSERT INTO table (title, ...'

Also, backticks (``) are used to let the database driver know that you're using this value and is not to be used as a reserved keyword. In other words, entirely obsolete in this query.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38