1

これは、perl とデータベースの両方を使用した初めてのプログラミングです。html フォームから mysql データベースに結果を入力する perl スクリプトを正確に記述する方法を決定する際に問題が発生しています。

この特定のスニペットが問題の原因です:

    # CONFIG VARIABLES
    $platform = "mysql";
    $database = "*****";
    $host = "localhost";
    $port = "3306";
    $user = "*****";
    $pw = "*****";

    # DATA SOURCE NAME
    $dsn = "dbi:$platform:$database:$host:$port";

    # PERL DBI CONNECT
    $connect = DBI->connect($dsn, $user, $pw);

    # PREPARE THE QUERY
    $query = "INSERT INTO result (name, console, character, series, bday, steam) VALUES (fname, favConsole, fCharacter, favSeries, birthday, steamAcc)";
    $query_handle = $connect->prepare($query);

    # EXECUTE THE QUERY
    #$query_handle->execute();

データベース名、ユーザー名、パスワードをアスタリスクに置き換えましたが、実際のコードには真の値が含まれています。

connect と execute の行で問題が発生する理由がわかりません。

また、ユーザーが送信ボタンを押すと情報が書き込まれるように、プロンプトを正確にどこに配置しますか? フォーム アクションとして execute を追加しようとしましたが、うまくいきませんでした。

これまでの私の仕事は次のとおりです。

    #!/usr/bin/perl

    use CGI qw( :standard );
    use DBI;
    use DBD::mysql;

    # CONFIG VARIABLES
    $platform = "mysql";
    $database = "*****";
    $host = "localhost";
    $port = "3306";
    $user = "*****";
    $pw = "*****";

    # DATA SOURCE NAME
    $dsn = "dbi:$platform:$database:$host:$port";

    # PERL DBI CONNECT
    $connect = DBI->connect($dsn, $user, $pw);

    # PREPARE THE QUERY
    $query = "INSERT INTO result (name, console, character, series, bday, steam) VALUES (fname, favConsole, fCharacter, favSeries, birthday, steamAcc)";
    $query_handle = $connect->prepare($query);

    # EXECUTE THE QUERY
    #$query_handle->execute();

    $dtd =
    "-//W3C//DTD XHTML 1.0 Transitional//EN\"
       \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";

    print( header() );

    print( start_html( { dtd => $dtd,
      title => "~Video Game Survey~" } 
      #style=>{"background-image:url(/images/blue_line_background.jpg) ";
      #}
      ) );
    #print( " <body style="background-image:url(/images/blue_line_background.jpg)">\n " );


    print( "<body>\n" );
    print(     "<div style = \"font-size: 14pt; font-weight: bold\">\n" );
    print(      "Please let us know a bit more about yourself.\n" );
    print(     "</div>\n" );
    print( "</body>\n" );

    print( "<br />\n " );

    #Start of the form.
    print( "<form method = \"post\" > \n" );

    #First question, asking the visitor's name.  Name will be stored in the database         but not displayed on the
    #survey results.
    print( "<p>\n" );
    print(      "First of all, what is your name?\n" );
    print(      "<input type = \"text\" name = \"fname\" placeholder = \"John Doe\" required />\n" );
    print( "</p>\n" );

    print( "<br /><br />\n" );

    #Second question, type dropdown list.
    print( "<img src = \"/images/favConsole.png\" />\n");
    print( "<p>\n" );
    print(      "Gaming comes in all sorts of shapes and sizes.  What is your favorite console?\n" );
    print( "</p>\n" );
    print( "<dd>\n" );
    print(      "<input type = \"text\" name = \"favConsole\" placeholder = \"Select a console\"
                   list = \"consoles\" required/>\n" );
    print(              "<datalist id = \"consoles\">\n" );
    print(                  "<option value= \"PC\">\n" );
    print(                  "<option value= \"Playstation 3\">\n" );
    print(                  "<option value= \"Xbox 360\">\n" );
    print(                  "<option value= \"Wii\">\n" );
    print(              "</datalist>\n" );
    print( "</dd>\n");

    print( "<br /><br />\n" );

    #Third question, type radio.  Default value: Mario
    print( "<img src = \"/images/favCharacter.png\" />\n" );
    print( "<p>\n" );
    print(      "Out of the following list, who is your favorite video game character?\n" );
    print( "</p>\n" );
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Mario\" checked><label>Mario</label></dd>\n" ); 
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Sephiroth\" ><label>Sephiroth</label></dd>\n" );
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Sonic\" ><label>Sonic</label></dd>\n" );
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Thrall\" ><label>Thrall</label></dd>\n" );
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Pacman\" ><label>Pacman</label></dd>\n" );
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Cloud\" ><label>Cloud</label></dd>\n" );
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Sylvanas\" ><label>Sylvanas</label></dd>\n ");
    print(      "<dd><input name = \"fCharacter\" type = \"radio\" value = \"Kerrigan\" ><label>Kerrigan</label></dd>\n ");
    print( "</dd>\n" );

    print( "<br /><br />\n" );

    #Fourth question, type dropdown.  Required.
    #Dropdown options will not appear unless the window is maximized in terms of height.
    print( "<img src = \"/images/favSeries.png\" />\n" );
    print( "<p>\n" );
    print(      "Which of the following series is your favorite?\n" );
    print( "</p>\n" );
    print(      "<input type = \"text\" name = \"favSeries\" placeholder = \"Select a series\"
                   list = \"series\" required/>\n" );
    print(              "<datalist id = \"series\">\n" );
    print(                  "<option value= \"Legend of Zelda\">\n" );
    print(                  "<option value= \"Halo\">\n" );
    print(                  "<option value= \"Bioshock\">\n" );
    print(                  "<option value= \"Resident Evil\">\n" );
    print(                  "<option value= \"Mario Party\">\n" );
    print(                  "<option value= \"Angry Birds\">\n" );
    print(                  "<option value= \"The Sims\">\n" );
    print(                  "<option value= \"Mass Effect\">\n" );
    print(                  "<option value= \"Half-Life\">\n" );
    print(                  "<option value= \"Warcraft\">\n" );
    print(                  "<option value= \"Starcraft\">\n" );
    print(                  "<option value= \"Borderlands\">\n" );
    print(              "</datalist>\n" );

    print( "<br /><br />\n" );

    #Fifth question, type date.  No default value, but still required.
    print( "<img src = \"/images/bday.png\" />\n" );
    print( "<p>\n" );
    print(      "What is your cake day?\n" );
    print( "</p>\n" );
    print(      "<input type = \"date\" name = \"birthday\" required />(yyyy-mm-dd)\n" );

    print( "<br /><br />\n" );

    #Sixth question, type radio.  Default value: no.
    print( "<p>\n" );
    print(      "Last but not least, do you have a Steam account?\n" );
    print(      "<input name = \"steamAcc\" type = \"radio\" value = \"Y\" >        <label>Yes</label>\n" );
    print(      "<input name = \"steamAcc\" type = \"radio\" value = \"N\" checked>        <label>No</label>\n" );
    print( "</p>\n" );

    print( "<p>\n" );
    print(      "<input type = \"submit\" value = \"Submit\" />\n" );
    print(      "<input type = \"reset\" value = \"Clear\" /></p>\n" );
    print( "</p>\n" );

    #End of the form
    print( "</form>\n" );

    print( end_html() );
4

2 に答える 2

1

最初にいくつかのこと:

  1. コードの安全のために、これらの行をスクリプトの先頭に追加してください。

    use strict;
    use warnings;
    
  2. my上記の手順は、すべての変数を次のように宣言する必要があることを意味します。

    my $dsn = ...
    
  3. 接続または実行に問題がある場合はエラーをスローするようにDBIに指示します(デフォルトではこれを行いません)。エラーメッセージから、何が起こっているのかを理解するための手がかりを得ることができます。

    my $dbh = DBI->connect($dsn, $user, $pw, {RaiseError => 1});
    
  4. SQLにプレースホルダーを使用します(これにより、SQLインジェクションが防止され、SQLに挿入値の引用符が自動的に追加されます)

    my $query = 'insert into (name, console) values (?, ?)';
    my $sth = $connect->prepare($query);
    $sth->execute('fname', 'favConsole');
    # this is equivalent to:
    # "insert into (name, console) values ('fname', 'favConsole')"
    
  5. 値はCGIパラメータから取得する必要があります(まだその段階に達していないようです)。

于 2012-12-18T03:49:45.800 に答える
0
$query_handle = $connect->prepare($query)

する必要があります

$query_handle = $dbh->prepare($query)

$something変数の名前です。という変数$connectがないため、明らかに正しくありません。その行は、データベース接続とクエリ文字列を受け取り、クエリを実行するために必要なすべての情報を含むオブジェクトであるクエリ ハンドルを返します。

于 2012-12-18T00:26:15.497 に答える