Correct way to sanitize input in MySQL using PDO

前端 未结 3 1436
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 02:03

so I had a friend of mine try to run a SQLinjection on my site and he managed to get into it using the code underneath. How can I prevent this? I have read something about s

3条回答
  •  误落风尘
    2021-01-01 02:26

    The idea of prepared statements is that you don't concatenate variables, instead you bind the parameters. The difference is the variable never gets inserted into the SQL, rather the MySQL engine handles the variable separately which leaves no possibility of SQL Injection. This also has the added bonus that no escaping or pre-processing of the variable is required.

    $query = $db->prepare("SELECT password FROM login WHERE username = :username");
    $query->execute(array(':username' => $username));
    

提交回复
热议问题