PHP MySQL 預處理語句

預處理語句對於防止 MySQL 注入是非常有用的。

SQL注入,就是透過把SQL指令插入Web表單遞交或輸入網域或頁面請求的查詢字串,最後達到欺騙伺服器執行惡意的SQL指令。


預處理語句及綁定參數

#預處理語句用於執行多個相同的SQL 語句,並且執行效率更高。

預處理語句的工作原理如下:

1.    預處理:建立 SQL 語句範本並傳送到資料庫。預留的值使用參數 "?" 標記 。例如:

INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)

2.    資料庫解析,編譯,對SQL語句模板執行查詢最佳化,並儲存結果不輸出。應用可以多次執行語句,如果參數的值不一樣。

3.    執行:最後,將套用綁定的值傳遞給參數("?" 標記),資料庫執行語句。

比起直接執行SQL語句,預處理語句有兩個主要優點

·    預處理語句大幅減少了分析時間,只做了一次查詢(雖然語句多次執行)。

·    綁定參數減少了伺服器頻寬,你只需要傳送查詢的參數,而不是整個語句。

·    預處理語句針對SQL注入是非常有用的,因為參數值發送後使用不同的協議,保證了資料的合法性。


MySQLi 預處理語句

##以下實例在MySQLi 中使用了預處理語句,並綁定了對應的參數:

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 
 // 创建连接
 $conn = new mysqli($servername, $username, $password, $dbname);
 
 // 检测连接
 if ($conn->connect_error) {
     die("连接失败: " . $conn->connect_error);
 }
 
 // 预处理及绑定
 $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)");
 $stmt->bind_param("sss", $firstname, $lastname, $email);
 
 // 设置参数并执行
 $firstname = "John";
 $lastname = "Doe";
 $email = "john@example.com";
 $stmt->execute();
 
 $firstname = "Mary";
 $lastname = "Moe";
 $email = "mary@example.com";
 $stmt->execute();
 
 $firstname = "Julie";
 $lastname = "Dooley";
 $email = "julie@example.com";
 $stmt->execute();
 
 echo "新记录插入成功";
 
 $stmt->close();
 $conn->close();
 ?>

解析下列實例的每行程式碼:

"INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"

在SQL 語句中,我們使用了問號(?),在此我們可以將問號替換為整數型,字串,雙精確度浮點型和布林值。

接下來,讓我們來看下bind_param() 函數:

$stmt->bind_param("sss", $firstname, $lastname, $email );

此函數綁定了SQL 的參數,並告訴資料庫參數的值。 "sss" 參數列處理其餘參數的資料類型,上面有幾個(?),下面就要有幾個資料類型,s 字元告訴資料庫該參數為字串。

參數有以下四種類型:

·    i - integer(整數型)

·   d - double(雙精確度浮點型)

#·     s - string(字串)

·   b - BLOB(binary large object:二進位大物件)

每個參數都需要指定型別。

關於怎麼指定資料類型,在上一節已經做了介紹

透過告訴資料庫參數的資料類型,可以降低 SQL 注入的風險。

上面的程式碼執行結果:

新記錄插入成功

#看看你的資料有沒有插入成功?


實例

#讓我們將表單裡的資料插入資料庫

首先一個HTML 頁面

<!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>PHP中文网</title>
 </head>
 <body>
 
 <form action="insert.php" method="post">
     Firstname: <input type="text" name="firstname" /><br/>
     Lastname: <input type="text" name="lastname" /><br/>
     email: <input type="text" name="email" /><br/>
     <input type="submit" />
 </form>
 </body>
 </html>

提交到php 頁面

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 
 // 创建连接
 $conn = new mysqli($servername, $username, $password, $dbname);
 
 // 检测连接
 if ($conn->connect_error) {
     die("连接失败: " . $conn->connect_error);
 }
 
 // 预处理及绑定
 $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)");
 $stmt->bind_param("sss", $firstname, $lastname, $email);
 
 // 设置参数并执行
 $firstname = $_POST['firstname'];
 $lastname =$_POST['lastname'];
 $email = $_POST['email'];
 $stmt->execute();
 
 echo "新记录插入成功";
 
 $stmt->close();
 $conn->close();
 ?>

透過上面的兩個程序,就可以將我們表單裡的資料插入資料庫了


繼續學習
||
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 预处理及绑定 $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"); $stmt->bind_param("sss", $firstname, $lastname, $email); // 设置参数并执行 $firstname = "John"; $lastname = "Doe"; $email = "john@example.com"; $stmt->execute(); $firstname = "Mary"; $lastname = "Moe"; $email = "mary@example.com"; $stmt->execute(); $firstname = "Julie"; $lastname = "Dooley"; $email = "julie@example.com"; $stmt->execute(); echo "新记录插入成功"; $stmt->close(); $conn->close(); ?>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!