How to bind parameters in mysql query?

前端 未结 1 900
南方客
南方客 2020-12-20 04:28

I have search for bind parameters. But it just getting me confused. I\'m really a beginner in php and mysql.

here is code:

$query =\"UPDATE table_use         


        
相关标签:
1条回答
  • 2020-12-20 05:00

    When you write the query, leave the values (the $_POST variables) out of the SQL code and in their place use a placeholder. Depending on which interface you're using in PHP to talk to your MySQL database (there's MySQLi and PDO), you can use named or unnamed place holders in their stead.

    Here's an example using PDO

    $query = "UPDATE table_user_skills SET rating= :ratings where rating_id= :id";
    $stmt = $conn->prepare($query);
    $stmt->execute($_POST);
    

    What we've done here is send the SQL code to MySQL (using the PDO::prepare method) to get back a PDOStatement object (denoted by $stmt in the above example). We can then send the data (your $_POST variables) to MySQL down a separate path using PDOStatement::execute. Notice how the placeholders in the SQL query are named as you expect your $_POST variables. So this way the SQL code can never be confused with data and there is no chance of SQL injection.

    Please see the manuals for more detailed information on using prepared statements.

    0 讨论(0)
提交回复
热议问题