2012-03-08 14 views
0

私はPHPとSQLを学習しています。このページでは、SQLインジェクションの防止を練習しようとしています。今は2つの変数で試しています。 ac1とac2。送信時にmysql()エラーが発生します。何が間違っていますか?PHP SQLインジェクション防止構文

<?php 
$host="localhost"; // Host name 
$username="******"; // Mysql username 
$password="******"; // Mysql password 
$db_name="******_practice"; // Database name 
$tbl_name="administration"; // Table name 

// Connect to server and select databse. 
$dbc = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

$ac1=$_POST['ac1']; 
$ac2=$_POST['ac2']; 
$fan=$_POST['fan']; 
$na=$_POST['na']; 
$dh=$_POST['dh']; 

$tolerance1=$_POST['tolerance1']; 
$temptime1=$_POST['temptime1']; 
$tolerance2=$_POST['tolernce2']; 
$temptime2=$_POST['temptime2']; 
$tolerance3=$_POST['tolerance3']; 
$temptime3=$_POST['temptime3']; 
$tolerance4=$_POST['tolerance4']; 
$temptime4=$_POST['temptime4']; 
$tolerance5=$_POST['tolerance5']; 
$temptime5=$_POST['temptime5']; 

$humidtolerance1=$_POST['humidtolerance1']; 
$humidtime1=$_POST['humidtime1']; 
$humidtolerance2=$_POST['humidtolerance2']; 
$humidtime2=$_POST['humidtime2']; 
$humidtolerance3=$_POST['humidtolerance3']; 
$humidtime3=$_POST['humidtime3']; 
$humidtolerance4=$_POST['humidtolerance4']; 
$humidtime4=$_POST['humidtime4']; 
$humidtolerance5=$_POST['humidtolerance5']; 
$humidtime5=$_POST['humidtime5']; 
// To prevent MySQL injection (a form of internet hacking) 
$ac1 = stripslashes($ac1); 
$ac2 = stripslashes($ac2); 
$ac1 = mysql_real_escape_string($ac1); 
$ac2 = mysql_real_escape_string($ac2); 


$custnum = 0; 
$sql="UPDATE {$tbl_name} SET ac1 = '{$ac1}', ac2 = '{$ac2}', fan = '{$fan}', na = '{$na}', da = '{$dh}', tolerance1 = '{$tolerance1}', temptime1 = '{$temptime1}',tolerance2 = '{$tolerance2}', temptime2 = '{$temptime2}',tolerance3 = '{$tolerance3}', temptime3 = '{$temptime3}',tolerance4 = '{$tolerance4}', temptime4 = '{$temptime4}',tolerance5 = '{$tolerance5}', temptime5 = '{$temptime5}', humidtolerance1 = '{$humidtolerance1}', humidtime1 = '{$humidtime1}',humidtolerance2 = '{$humidtolerance2}', humidtime2 = '{$humidtime2}',humidtolerance3 = '{$humidtolerance3}', humidtime3 = '{$humidtime3}',humidtolerance4 = '{$humidtolerance4}', humidtime4 = '{$humidtime4}',humidtolerance5 = '{$humidtolerance5}', humidtime5 = '{$humidtime5}' WHERE custnum = '{$custnum}'"; 
    $result = mysql_query($sql) 
    or die('Error querying database.'); 


//Send them back to the page they were at/ 
header("location:index.php"); 
?> 
+0

あなた自身の利益のために、 'die'メッセージのどこかに 'mysql_error()'の内容をエコーし​​てみるべきです。特定の問題をデバッグするのがずっと簡単になります。 – Gian

+1

クエリに 'mysql_real_escape_string'を使ってサニタイズされていない変数がかなりあるようです。 '$ _POST'から来るあなたのクエリの中の全ての変数は、' $ ac1'と '$ ac2'と同じ方法で消されるべきです。エラーを調べるには、 'die'出力にmysql_error()を追加してみてください。コードが実稼働状態になるときにこれを削除してください。エンドユーザーにデータベースに関する情報を出力する必要はありません。 – Travesty3

+0

エラーの内容を説明するために 'mysql_error()'をエコーし​​てみてください。これまでの回答と同様に、私はPDOを見ることをお勧めします。 – AndrewR

答えて

0

mysqliを使用するか、さらにはPDOと準備文を使用してください。 mysql_queryを直接呼び出すことは、通常、最良の方法ではありません。

http://php.net/manual/en/book.pdo.php

2

あなたはSQLインジェクションを防ぐことがあろうと、もう少し必ずしたい場合は、古い(代わりのmysql_real_escape_stringを使用して(あなたがPHP 5.1以降を使用して提供される)PDO prepared statementsを使用して検討する必要があります道)

それはオプションではありません場合:

  • あなたは0custnumがあることを確認していますあなたのデータベースに?多くの場合、データベースインデックスは1で始まる傾向があります。 その場合は、クエリが失敗していない理由の一つかもしれ
+0

括弧はどのような害を及ぼしますか? – Travesty3

+0

@ Travesty3私は彼らがここで必要とは思わない。二重引用符で囲まれた文字列の中で多次元配列にアクセスしているときに使用します。そうでなければ、必要ないように見えます。 – summea

+0

おそらく必要ないかもしれませんが、私はそれは悪い考えではないと思います。私はPHP変数が文字列に挿入されているところでそれらをどこでも使っています。それは私のエディタで簡単に目立つようになり、より一貫しています。 – Travesty3

0

は、あなたのケースでは、引用符でなければなりません(更新することは何も。):

$sql="UPDATE {$tbl_name} SET `ac1` = '{$ac1}', `ac2` = '{$ac2}', `fan` = '{$fan}', `na` = '{$na}', `da` = '{$dh}', `tolerance1` = '{$tolerance1}', `temptime1` = '{$temptime1}',`tolerance2` = '{$tolerance2}', `temptime2` = '{$temptime2}',`tolerance3` = '{$tolerance3}', `temptime3` = '{$temptime3}',`tolerance4` = '{$tolerance4}', `temptime4` = '{$temptime4}',`tolerance5` = '{$tolerance5}', `temptime5` = '{$temptime5}', `humidtolerance1` = '{$humidtolerance1}', `humidtime1` = '{$humidtime1}',`humidtolerance2` = '{$humidtolerance2}', `humidtime2` = '{$humidtime2}',`humidtolerance3` = '{$humidtolerance3}', `humidtime3` = '{$humidtime3}',`humidtolerance4` = '{$humidtolerance4}', `humidtime4` = '{$humidtime4}',`humidtolerance5` = '{$humidtolerance5}', `humidtime5` = '{$humidtime5}' WHERE `custnum` = '{$custnum}'"; 

すなわち、 のフィールド名をとしてください。の値は1つで、です。それにもかかわらず、アドバイスをお願いします。プロダクションモードでは、以前のアドバイスと同じように、PDOの使用をお勧めします。幸運

+0

値に一重引用符を使用しても、フィールド名の前後にバッククォートは必要ありません。バッククォートは、[代​​替文字の使用を許可する](http://stackoverflow.com/a/261476/259457)と予約語です。 – Travesty3

関連する問題