我是非常新的php而且我只是在玩准备好的语句。我想转换一个包含几个易受攻击的SQL查询的函数。我必须将此转换为准备好的语句。如何做到这一点
<?php
class Users {
public $tableName = 'users';
function __construct(){
//database configuration
$dbServer = 'localhost'; //Define database server host
$dbUsername = 'root'; //Define database username
$dbPassword = ''; //Define database password
$dbName = 'live'; //Define database name
//connect databse
$con = mysqli_connect($dbServer,$dbUsername,$dbPassword,$dbName);
if(mysqli_connect_errno()){
die("Failed to connect with MySQL: ".mysqli_connect_error());
}else{
$this->connect = $con;
}
}
function checkUser($oauth_provider,$oauth_uid,$fname,$lname,$email,$gender,$locale,$link,$picture){
$prevQuery = mysqli_query($this->connect,"SELECT * FROM $this->tableName WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
if(mysqli_num_rows($prevQuery) > 0){
$update = mysqli_query($this->connect,"UPDATE $this->tableName SET oauth_provider = '".$oauth_provider."', oauth_uid = '".$oauth_uid."', fname = '".$fname."', lname = '".$lname."', email = '".$email."', gender = '".$gender."', locale = '".$locale."', picture = '".$picture."', gpluslink = '".$link."', modified = '".date("Y-m-d H:i:s")."' WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
}else{
$insert = mysqli_query($this->connect,"INSERT INTO $this->tableName SET oauth_provider = '".$oauth_provider."', oauth_uid = '".$oauth_uid."', fname = '".$fname."', lname = '".$lname."', email = '".$email."', gender = '".$gender."', locale = '".$locale."', picture = '".$picture."', gpluslink = '".$link."', created = '".date("Y-m-d H:i:s")."', modified = '".date("Y-m-d H:i:s")."'") or die(mysqli_error($this->connect));
}
$query = mysqli_query($this->connect,"SELECT * FROM $this->tableName WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
$result = mysqli_fetch_array($query);
return $result;
}
}
?>
答案 0 :(得分:1)
我只是为您提供演示。我想你能从这里找到自己的路。
$db = new mysqli($server, $username, $password, $dbname);
//check for errors
//Now we prepare the sstatement
$stmt = $db->prepare("INSERT INTO tablename (uid, email, name) VALUES (?, ?, ?)");
//bind the parameters
$stmt->bind_param('iss', $uid, $email, $name);
//now we can use this as many times as we want
$uid=123;
$email="mail1@gggg.com";
$name="Joe";
$stmt->execute();
//here we go again
$uid=124;
$name="Jack";
$email="jack@gggg.com";
$stmt->execute();