我有注册表格,该表格将用户信息保存在'tblusers'->'Username'中,但是它不会检查用户是否已经存在,因此目前用户可以使用相同的用户名进行注册,这是一个很大的麻烦。

因此,我需要在注册时检查MySQL数据库中是否存在现有的“用户名”。任何想法如何使用PHP做到这一点-MySQL,甚至是javascript。

这是我正在表单提交上执行的代码:

    check_token ();
    $_SESSION['currency'] = $currency;
    $clientid = addclient ($firstname, $lastname, $companyname, $email, $address1, $address2, $city, $state,
        $postcode, $country, $phonenumber, $password, $securityqid, $securityqans, $sendemail);
    //print "New clientid=" . $clientid . "<br>";
    update_query ('tblclients', array ('notes' => $notes, 'status' => $status, 'credit' => $credit, 'salesOperatorId' => $salesOperatorId,
        'taxexempt' => $taxexempt, 'latefeeoveride' => $latefeeoveride, 'overideduenotices' => $overideduenotices, 'country' => 'US',
        'language' => $language, 'billingcid' => $billingcid, 'lastlogin' => '00000000000000'), array ('id' => $clientid));
    //print "New clientid now=" . $clientid . "<br>";
    savecustomfields ($clientid, $customfield);
    $result = select_query ('tblusers', 'ID', array ('ClientID' => $clientid));

    // adding user to the DB at same time as client is being added ...
    $datenowtimestamp = date("Y-m-d H:i:s");
    $table91 = 'tblusers';
    $array91 = array ('ClientID' => $clientid, 'firstname' => $userfirstname, 'lastname' => $userlastname, 'email' => $useremail,
            'username' => $username, 'password' => $password, 'searchdelivery' => 'Criminal', 'reporttiming' => 'Instant',
            'deliverymethod' => 'email', 'Created' => $datenowtimestamp,
            'phonenumber' => $phonenumber, 'orderreports' => 1, 'viewallreports' => 1, 'viewpricing' => 1,
            'restrictreportview' => 0, 'gatewayaccess' => 0);
    insert_query ($table91, $array91);

    if($data = mysql_fetch_array ($result))
        $userid = $data['ID'];
    $header = 'Location: clientsusers.php?clientid=' . $clientid;
    //print $header . "<br>";
      header ($header);
      exit ();

最佳答案

正确的方法可能是通过在用户表的username列上添加唯一索引:

CREATE UNIQUE INDEX username_index ON users (username);


另一种方法(签入您的PHP代码)可能导致两个人试图同时使用相同的用户名注册的问题。

07-24 09:37
查看更多