简体   繁体   English

选择和插入数据时保护php和sql

[英]Secure php and sql when selecting and inserting data

I have an app that takes data from MySQL database and also inserting data into it (the user is writing the data that is getting inserted) and honestly I am pretty new to php and don't know a lot about securing and sanitizing strings, I want to make the php files more secure and I don't know what to look for in order of doing it, if someone can send a tutorial it will be great. 我有一个应用程序,该应用程序从MySQL数据库中获取数据并向其中插入数据(用户正在编写要插入的数据),说实话,我对php还是很陌生,对字符串的保护和清理不了解很多,我想要使php文件更安全,而且我不知道该按什么顺序查找,如果有人可以发送教程,那就太好了。

here is the select and insert codes 这是选择和插入代码

    <?php
header('Content-Type: text/html; charset=utf-8');

    $db = "*********";
    $username = "*********";
    $password = "*******";
    $host = "************";

$sql = "select * from sample;";


$conn = mysqli_connect($host,$username,$password,$db);

$conn->set_charset('utf8');




$result = mysqli_query($conn,$sql);

$response = array();

while($row = mysqli_fetch_array($result))
{
    array_push($response,array($row[0],$row[1],$row[2]));
}
$str = json_encode(array($response),JSON_UNESCAPED_UNICODE);
$str = clean($str);
echo $str;
mysqli_close($conn);


function clean($string) {
    $string = str_replace(' ', ' ', $string);
    $string = preg_replace('/[^a-zA-Z0-9,×-×–, : . -]/', '', $string);
    return preg_replace('/-+/', '-', $string);
}

?>

and the insert: 和插入:

<?php

    $db = "*********";
    $username = "*********";
    $password = "*******";
    $host = "************";
$conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);


$name =$_POST["name"];
$publisher=$_POST["publisher"];
$date=$_POST["date"];


$sql_query = "insert into sample(name,publisher,date)
             values('$name','$publisher','$date');";

if(mysqli_query($conn,$sql_query))
{
echo "data inserted";
}
else
{
echo "error";
}
?>

Use prepared statements any time possible: 随时使用准备好的语句:

$sql_query = "insert into sample(name,publisher,date) values(?,?,?);";

$stmt = mysqli_prepare($conn,$sql_query);

mysqli_stmt_bind_param( $stmt , "sss" , $name,$publisher,$date);

mysqli_stmt_execute($stmt);

And try to use the object style only, not the procedural of the mysqli extention. 并尝试仅使用对象样式,而不使用mysqli扩展的过程。

You are mixing both here: 您在这里混合两者:

$conn = mysqli_connect($host,$username,$password,$db);//procedural style

$conn->set_charset('utf8');//oject style

You can use PDO. 您可以使用PDO。 It's very simple to build safe SELECT and INSERT queries. 构建安全的SELECT和INSERT查询非常简单。 Although, you must be careful on some commands such as ORDER BY. 虽然,您必须小心某些命令,例如ORDER BY。

<?php

$pdo = new PDO('mysql:host=localhost;dbname=databasename;charset=utf8', 'username', 'password');
$statement = $pdo->prepare("SELECT * FROM users WHERE firstname = :firstname AND lastname = :lastname");
$statement->execute(array(':firstname' => 'Max', ':lastname' =>  'Mustermann'));  

if( $statement->rowCount() > 0 ) { 
   $row = $statement->fetch();
   echo "Hello " . $row['firstname'];
}

?>

Mysqli can be used too, but please check out mysqli_real_escape_string. 也可以使用Mysqli,但是请签出mysqli_real_escape_string。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM