我從一個研究所學到了PHP,但他們教會了我將PHP插入數據庫的舊語法和技巧。當我在網上搜索時發現它不推薦使用,並且不會在將來的PHP版本中使用。我想了解將數據插入數據庫的新技術。我給你舉例說明我現在在做什麼,它對我來說工作正常,但我想用準備好的語句和所有可能的技術插入數據。PHP舊語法問題
HTML:
<form method="post" action="do_submit.php"/>
Name:<input type="text" name="name" id="name"/>
Class:<input type="text" name="class" id="class"/>
Section:<input type="text" name="section" id="section"/>
Roll Number:<input type="text" name="roll" id="roll"/>
Registration Number:<input type="text" name="reg" id="reg"/>
<input type="submit"/>
</form>
do_submit.php:
<?php
include 'dbconnect.php';
$name=$_POST['name'];
$class=$_POST['class'];
$section=$_POST['section'];
$roll=$_POST['roll'];
$reg=$_POST['reg'];
$sql = mysql_query("INSERT INTO `school`.`students` (`Name`, `Class`, `Section`,`Roll_No`, `Reg_No`)
VALUES ('$name', '$class', '$section', '$roll','$reg');") or die("SELECT Error: ".mysql_error());
if($sql)
{
$myURL = 'success.php?sType=insert';
header('Location: '.$myURL);
exit;
}
else
echo "Try again!";
?>
任何人都可以請引導我,示例代碼,使我瞭解到更被黑客獲得新技術。
給看看[PDO](http://php.net/manual/en/book.pdo.php),並嘗試使用準備語句已經更安全 –
可能重複的[我怎樣才能防止SQL注入PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php ) – andrewsi