【发布时间】:2019-02-27 09:35:10
【问题描述】:
我发现为我的搜索表单编写 SQL 准备语句很困难,我可以得到修复它的帮助吗?没有 SQL 准备的绑定语句,一切都很好,但我确信它不是那么安全。
这是我的代码:
<?php
// Define Database connection parameters
$dbserver = "localhost";
$username = "root";
$password = "";
$dbname = "student";
// Lets Connect to theDatabase Table, But if there is an Error lets tell before its too late to figured
$conn = mysqli_connect ( $dbserver, $username, $password, $dbname ) or die ( ' I can not connect to the database ' );
// Its time to Capture the varibles and user inpute from the form , also we need to sanitize the input to avoid SQL Injection
$study_group = mysqli_real_escape_string ( $conn, $_POST['matric_number']);
/* Lets try to use bind Statement to reduce further hacking
I am also avoiding using "LIKE" Clause because IVariable direct Exact results so will be using the Direct Varible
*/
$sql = $conn->prepare (" SELECT * FROM study_circle WHERE matric = ? ") ;
$sql->bind_param('s', $study_group);
$sql ->execute();
$results = mysqli_query ($conn, $sql);
$mysqlResults = mysqli_num_rows ($results);
if ( $mysqlResults > 0 )
{
while ( $row = mysqli_fetch_assoc ( $results ))
{
// Display results in table form
echo " <div>
<h4> ".$row['full_name']."</h4>
</div>";
}
} else {
echo " Please Ensure your Matric Number is correct, We can not find anything relting to your data";
}
【问题讨论】:
-
你的错误是什么?
-
当你使用准备好的语句时,你不应该转义你的值,现在它会检查一个被引用的值。
-
$results = mysqli_query ($conn, $sql);。此时,$sql是一个语句对象,而不是包含 SQL 查询的字符串。该行也毫无用处,因为您之前已经进行了几行查询。
标签: php sql sql-injection