我是PHP和數據庫編程的新手,一直在嘗試將表單中的數據添加到MySQL數據庫中。它工作正常,但是這對我的MySQL注入是開放的嗎?我讀過很多教程,我在想PDO準備好的語句。例如,我如何爲我的評論欄做到這一點?這個字段(它是一個文本字段)對於用戶想要放置的任何內容都是相當開放的。我怎麼寫這個以使它更安全?如何使這個插入從MySQL注入安全?
<?php
ob_start();
$username = 'name';
$password = 'pass';
$host = 'localhost';
$dbname = 'map';
try {
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO Incidents (
protocol,
jurisdiction,
date,
time,
comments,
video,
lat,
lng
)
VALUES (
'".$_POST["protocol"]."',
'".$_POST["jurisdiction"]."',
'".$_POST["date"]."',
'".$_POST["time"]."',
'".$_POST["comments"]."',
'".$_POST["video"]."',
'".$_POST["lat"]."',
'".$_POST["lng"]."'
)
";
// use exec() because no results are returned
$dbh->exec($sql);
header("Location: map1.php");
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbh = null;
ob_end_flush();
?>
是指這樣的:http://stackoverflow.com/questions/7043303/php-mysql-injection-protection –