0

このように設定されたデータベースにデータを挿入したい

テーブル

  • 名前 varchar(35)
  • 部門 varchar(50)
  • 評価 smallint(6)
  • 容易さ smallint(6)
  • 教科書 varchar(3)
  • タイムスタンプ タイムスタンプ
  • コース char(10)
  • 曲線文字(3)
  • コメント テキスト

以下のようなクエリを使用してデータを挿入しましたが、実際にはデータベースに挿入されず、ページがリロードされ、それだけです。print_r()との結果を次に示しvar_dump()ます。実際のクエリ関数は次のとおりです。何が問題になる可能性がありますか? ある種のテキストタイプのオーバーフローである可能性があると思います。しかし、私はそれを理解することはできません、あなたの助けに感謝します

print_r()結果:

insert into professor(name,department,rating,easiness,textbook,course,curve,comment)values('Cherry, Mark','HUMANITIES','8','8','yes','PHIL 3311','no','Go to class. He doesn't require it but it will make the difference between an A or a B. It's very difficult to do anything without being in class. He gives all the answers you need so long as you show up. He's a very good teacher and I would recommend him to someone else')

var_dump()結果:

string(424) "insert into professor(name,department,rating,easiness,textbook,course,curve,comment)values('Cherry, Mark','HUMANITIES','8','8','yes','PHIL 3311','no','Go to class. He doesn't require it but it will make the difference between an A or a B. It's very difficult to do anything without being in class. He gives all the answers you need so long as you show up. He's a very good teacher and I would recommend him to someone else')" bool(false)   

これが私のコードです:

function processadd()
{
    $name =htmlentities($_POST['prof']);
    $department =htmlentities($_POST['depart']);
    $rating =htmlentities($_POST['Rating']);
    $easy=htmlentities($_POST['Easiness']);
    $textbook =htmlentities($_POST['Book']);
    $course =htmlentities($_POST['course']);
    $curve =htmlentities($_POST['curve']);
    $comment =htmlentities($_POST['comment']);
    if($course == "" || $comment == "")
    {
        print"<div class=error><h3> Empty Fields exist!, please fill out completely</h3></div>";
    }
    else
    {
    $db = adodbConnect();
    $query = "insert into professor(name,department,rating,easiness,textbook,course,curve,comment)values('$name','$department','$rating','$easy','$textbook','$course','$curve','$comment')";
    $result = $db -> Execute($query);
    }
    print_r($query);
    print_r($result);
    print"<br>";
    print"<br>";
    print"<br>";
    var_dump($query);
    var_dump($result);

}
4

2 に答える 2

0

あなたの問題はコメントにある可能性があります。コメント全体で 's をエスケープしていないようです。次のステートメントを挿入してみてください。

insert into professor(name,department,rating,easiness,textbook,course,curve,comment)
values('Cherry, Mark','HUMANITIES','8','8','yes','PHIL 3311','no','Go to class. He doesn\'t require it but it will make the difference between an A or a B. It\'s very difficult to do anything without being in class. He gives all the answers you need so long as you show up. He\'s a very good teacher and I would recommend him to someone else');
于 2012-12-17T05:51:04.350 に答える
0

受け取ったエラーを指定していませんが、クエリの引用符、特に (ただしこれに限定されません)comment列をエスケープしないことに関係していると思います。

たとえば、サンプルの列は次のとおりです。

授業に行く。彼はそれを必要としませんが、それは A または B の違いになります。クラスに参加せずに何かをすることは非常に困難です。あなたが現れる限り、彼はあなたが必要とするすべての答えを与えます. とても良い先生で、他の人にも勧めたいです

このテキストにはいくつかの一重引用符が含まれているため、SQL クエリが壊れてエラーが発生します。

各列の入力で使用htmlentitiesしていますが、これは SQL インジェクションを防ぎません。デフォルトでhtmlentitiesは、単一引用符も変換されません。

データベース クラスがどの MySQL ライブラリを実装しているかはわかりませんが、ライブラリのエスケープ関数を使用してみてください。

たとえば、古いmysql_関数を使用している場合は、各値mysql_real_escape_string()の代わりに使用します。htmlentities()一方、より推奨されるMySQLiまたはPDOクラスを使用している場合は、代わりにクエリを準備済みステートメントに変換してください。

于 2012-12-17T05:52:20.560 に答える