0
我对PHP很陌生,并被告知我的MySQL语句对注入不安全。保护我的MySQL查询
有我的老查询:
$addbook = "INSERT INTO bookings (bookID, startDate, startTime, endDate, endTime) ";
$addtempres .= "VALUES ('".$bookid."', '".$startdate."', '".$starttime."', '".$enddate."', '".$endtime."')";
$insertBook = mysql_query($addbook);
$getblogposts = mysql_query("SELECT * FROM blogposts WHERE deleted = 'no' ORDER BY postID DESC LIMIT 4");
阅读它,我明白他们是不安全的,我也明白的mysql_query旧,并且正在贬值。
然而,我写了很多这些查询,并意识到他们都是旧的,不安全的是令人生畏的,所以我开始试图保护他们。
所以我这样做:
$escapedbookid = mysql_real_escape_string($bookid) ;
$escapedstartdate = mysql_real_escape_string($sqlcoldate);
$escapedstarttime = mysql_real_escape_string($forstarttime);
$escapedenddate = mysql_real_escape_string($sqlretdate);
$escapedendtime = mysql_real_escape_string($forendtime);
$escapedactive = mysql_real_escape_string('false');
$addtembook = "INSERT INTO bookings (bookID, startDate, startTime, endDate, endTime) ";
$addtempres .= "VALUES ('".$escapedbookid."', '".$escapedstartdate."', '".$escapedstarttime."', '".$escapedenddate."', '".$escapedendtime."')";
$insertRes = mysql_query($addtempbook);
这是更安全?我很欣赏PDO准备好的声明,当我学会将当前的查询翻译成它们时,它更容易也更安全,但我只是想知道我正在做的是让事情更安全还是不安全?
'mysql_'代码很快就会被弃用,原因很多,安全就是其中之一。我强烈建议你看看mysqli:http://us2.php.net/manual/en/mysqli.overview.php – Andy 2013-03-14 11:50:47
[**请不要在新代码**中使用'mysql_ *'函数](http ://bit.ly/phpmsql)。他们不再保留[并正式弃用(https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://www.brightmeup.info/article.php?a_id=2)。 – insertusernamehere 2013-03-14 11:52:11
@Andy是否也让mysqli使事情变得更加安全,还是只跟上最佳实践? – TryingToBeZen 2013-03-14 11:53:53