0

MYDAC コンポーネントを使用してユーザー情報をデータベースに保存するユーザー登録プロセスがあります。現在、重複ユーザーを許可していますが、これは私の意図ではありません。私のコードは以下のとおりですが、どこに問題があるのか​​ わかりません。

procedure TForm1.Button1Click(Sender: TObject);
begin    
  if (edit1.Text <> '') and (edit2.Text <> '') and (edit3.Text <> '') and 
    (edit4.Text <> '') then
  begin
    MyQuery1.Close;
    MyQuery1.SQL.Text := 'select * from uyeler '+
                         'where nick=:0 and mail=:0 and site=:0';

    MyQuery1.Params[0].AsString:=edit1.text;
    MyQuery1.Params[0].AsString:=edit2.text;
    MyQuery1.Params[0].AsString:=edit3.text;

    MyQuery1.open;

    if MyQuery1.RecordCount = 0 then
      MessageDlg('The same information! Try again.', mtError, [mbOK], 0)
    else
      MyQuery1.Close;

    MyQuery1.SQL.Text := 'INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
                                            '(:nick, :mail, :site, :sifre)';

    MyQuery1.ParamByName('nick').AsString := Edit1.text;
    MyQuery1.ParamByName('mail').AsString := Edit2.text;
    MyQuery1.ParamByName('site').AsString := Edit3.text;
    MyQuery1.ParamByName('sifre').AsString := Edit4.text;
    MyQuery1.Execute;

    Button1.Enabled := False;
    MessageDlg('Mission complate!', mtInformation, [mbOK], 0);

    Edit1.Clear;
    Edit2.Clear;
    Edit3.clear;
    Edit4.Clear;

    PageControl2.Visible := False;
    PageControl1.Visible := True;
  end
  else
  begin
    MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);    
  end;
end;

同じものでサインアップしないようにするにはどうすればよいですか? この場合、どうすればよいですか?

4

3 に答える 3

3

通常、これを強制するために、基礎となる MySQL テーブルで一意のインデックスを使用します。

于 2012-08-01T19:07:25.483 に答える
1

間違った結果をチェックしています。テストを次のように変更する必要があります

if MyQuery1.RecordCount > 0 then // At least one match found already

さらに良いことに、それMyDacをサポートしている場合は、使用することです

if not MyQuery1.IsEmpty then  // row already exists.

実際には、それよりも多くの問題があります。現在、常にメソッドの挿入部分を実行しているため、不一致beginのブロックがあります。end@TLama が言うように、同じ pameter を複数回使用して、nickmail、およびsiteすべて同じ値を割り当てています。代わりに名前付きパラメーターを使用してください (以下の SQL とパラメーターの割り当ての両方で示されています)。

procedure TForm1.Button1Click(Sender: TObject);
var
  UserExists: Boolean;
begin
  Button1.Enabled:=false;

  if (edit1.Text <> '') and (edit2.Text <> '') and 
     (edit3.Text <> '') and (edit4.Text <> '') then
  begin
    MyQuery1.Close;
    MyQuery1.SQL.Text :=' select* from uyeler '+
                        'where nick=:nick and mail=:mail and site=:site';

    MyQuery1.ParamByName('nick').AsString:=edit1.text;
    MyQuery1.ParamByName('mail').AsString:=edit2.text;
    MyQuery1.ParamByName('site').AsString:=edit3.text;
    MyQuery1.open;
    try
      UserExists := not MyQuery1.IsEmpty;
    finally
      MyQuery1.Close;
    end;

    if UserExists then
      MessageDlg('The same information! Try again.', mtError,[mbOK],0)
    else
    begin            // <<--- Added begin
      MyQuery1.SQL.Text :=' INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
                                              '(:nick, :mail, :site, :sifre)';

      MyQuery1.ParamByName('nick').AsString        := Edit1.text;
      MyQuery1.ParamByName('mail').AsString        := Edit2.text;
      MyQuery1.ParamByName('site').AsString        := Edit3.text;
      MyQuery1.ParamByName('sifre').AsString        := Edit4.text;
      try
        MyQuery1.Execute;
      finally
        MyQuery1.Close;
      end;
    end;                      // <------------ Moved end from below where marked

    MessageDlg('Mission complate!', mtInformation,[mbOK],0);

    Edit1.Clear;
    Edit2.Clear;
    Edit3.clear;
    Edit4.Clear;

    PageControl2.Visible:=false;
    PageControl1.Visible:=true;
  end            // <------------- removed extra end caused by addition above
  else
   MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);
end;
于 2012-08-01T19:06:27.463 に答える
-1

IMO @Ken Whiteによって投稿された回答は問題なく機能するはずですが、問題が発生しているためです。このコードを試してみることをお勧めします。クエリの実行における違いです。

フィールドのデータ型をCharまたはVarCharと見なしているため、データ値を入力するときに「」

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Enabled:=false;

  if (edit1.Text <> '') and (edit2.Text <> '') and 
     (edit3.Text <> '') and (edit4.Text <> '') then
  begin
    MyQuery1.SQL.Clear;
    MyQuery1.SQL.Add(' select* from uyeler where nick="'+edit1.text+'"' +
                      'and mail="'+edit2.text+'" and site="'+edit3.text+'"');
    MyQuery1.Execute;

    if not MyQuery1.IsEmpty then   //--- can also use MyQuery1.RecordCount >0 
      MessageDlg('The same information! Try again.', mtError,[mbOK],0)
    else
    begin            //--- no duplicates present 
      MyQuery1.SQL.Clear;
      MyQuery1.SQL.Add(' INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
                            '("'+edit1.text+'", "'+edit2.text+'","'+edit3.text+'", "'+edit4.text+'")');

     try
        MyQuery1.Execute;
      finally
        MyQuery1.SQL.Clear;
      end;

     MessageDlg('Mission complate!', mtInformation,[mbOK],0);

     Edit1.Clear;
     Edit2.Clear;
     Edit3.clear;
     Edit4.Clear;

     PageControl2.Visible:=false;
     PageControl1.Visible:=true;
    end;                        
  end;                 
  else
   MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);
end;
于 2012-08-03T06:09:35.153 に答える