我已經在PHP中使用此代碼將數據插入到數據庫中。我在注入部分非常新,我要求你檢查這些代碼是否能夠防止黑客入侵,如果沒有,請提供需要合併的適當更改。
檢查代碼是否受到保護
<head>
<title>abcde</title>
</head>
<body>
<?php
if(isset($_POST['update'])) {
$dbhost = 'host';
$dbuser = 'user';
$dbpass = $_POST['password'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass . "pswd");
if(! $conn) {
die('Could not connect: ' . mysql_error());
}
$Name = $_POST['Name'];
$XP = $_POST['XP'];
$TH = $_POST['TH'];
$sql = "insert into warriors (Name, XP, TH) values ('$Name','$XP','$TH')";
mysql_select_db('db');
$retval = mysql_query($sql, $conn);
if(! $retval) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}else {
?>
<h1>My Clan Details</h1>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border =" 0" cellspacing = "1"
cellpadding = "2">
<tr><td width = "100">Password</td>
<td><input name= "password" type = "password" id = "password"></td></tr>
<tr>
<td width = "100">Name</td>
<td><input name = "Name" type = "Name"
id = "Name"></td>
</tr>
<tr>
<td width = "100">XP</td>
<td><input name = "XP" type = "XP"
id = "XP"></td>
</tr>
<tr>
<td width = "100">Town Hall</td>
<td><input name = "TH" type = "TH"
id = "TH"></td>
</tr>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<input name = "update" type = "submit"
id = "update" value = "Submit">
</td>
</tr>
</table>
</form>
<?php
}
?>
如果可能的話,請提供鏈接/來源在那裏我可以提高我的編碼有關SQL注入。對你的寶貴時間感謝Thanx。
你應該看看綁定的參數和使用PDO庫,這是這裏的最佳實踐。使用字符串插值創建查詢不是要走的路。 http://php.net/manual/en/book.pdo.php –