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.

其实,你有更多的问题,虽然。你有一个不匹配beginend阻塞,因为现在你总是在运行方法的插入部分。正如@TLama 所说,您还多次使用相同的参数,分配nick,mailsite所有相同的值。改用命名参数(在 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 回答