質問

サーバー上のテーブルにデータを挿入する単純なフォームがあります。これを処理するために、挿入権限のみを持つ特別なユーザーを設定しました。接続エラーと構文エラーが発生します。

私のフォームは次のとおりです。

<form id="form1" name="form1" method="post" action="mailform.php" onsubmit="return validateForm();">

    <input type="text" id="First" maxlength="100" autocorrect placeholder="First name" />
    <input type="text" id="Last" maxlength="100" autocorrect placeholder="Last name" />
    <input type="text" id="Email" maxlength="100" autocorrect placeholder="Email address" />
    <select name="SalesPerson">
        <option value="SP1">SP1</option>
        <option value="SP2">SP2</option>
        <option value="SP3">SP3</option>
        </select>
    <select name="Show">
        <option value="Show1">Show1</option>
        <option value="Show2">Show2</option>
        </select>

        <button type="submit" id="submit" class="oneup">Submit</button>

</form>

そして mailform.php には次のものがあります:

<?php

    $name = "xxx_xxx";
    $name = mysql_real_escape_string($name);
    $SQL = "SELECT * FROM users WHERE username = '$name'";

$con = mysql_connect("localhost","xxx_xxx","xxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxx_x", $con);

$sql="INSERT INTO email_signup (First, Last, Email, SalesPerson, Show)
VALUES
('$_POST[First]','$_POST[Last]','$_POST[Email]','$_POST[SalesPerson]','$_POST[Show]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con)
?>

そして、ここにエラーがあります -

Warning: mysql_real_escape_string() [<a href='function.mysql-real-escape-string'>function.mysql-real-escape-string</a>]: Access denied for user 'xxx'@'localhost' (using password: NO) in <b>.../mailform.php</b> on line 28

Warning: mysql_real_escape_string() [<a href='function.mysql-real-escape-string'>function.mysql-real-escape-string</a>]: A link to the server could not be established in <b>.../mailform.php</b> on line 28 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Show) VALUES ('','','','SP1','Show1')' at line 1

接続の問題が発生する理由はありますか?ほぼ同じフォームを別の場所にセットアップしましたが、問題なく機能します。

役に立ちましたか?

解決

最初に接続を確立し、次に mysql_real_escape_string() を実行し、次にクエリを実行します。mysql_real_escape_string() は実際にデータベースに接続して文字列をエスケープさせます。接続していない場合は機能しません

他のヒント

最初に接続を試してください。

$con = mysql_connect("localhost","xxx_xxx","xxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


 $name = "xxx_xxx";
    $name = mysql_real_escape_string($name);
    $SQL = "SELECT * FROM users WHERE username = '$name'";

注記:この回答は、SQL インジェクションの重大な脆弱性に対処するものではありません。さらに詳しい議論については、質問の下にあるコメントを読んでください。

見せる は予約語です

使用

$sql="INSERT INTO email_signup (`First`, `Last`, `Email`, `SalesPerson`, `Show`)
VALUES
('$_POST[First]','$_POST[Last]','$_POST[Email]','$_POST[SalesPerson]','$_POST[Show]')";
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top