判断数据库里面是否有重名的?

#1 xiaorong321

function write(){ // 这里是留言
               
                $conditions = array( 'user_id' => 'xiaorong321' );
                $user = spClass("test");
                $result = $test->findCount($conditions); // 使用了findCount
                echo $result;
                if($result==0)
                {
                                $newrow = array( // 这里制作新增记录的值
                                'user_id' => $this->spArgs('user_id'),
                                'pwd' => $this->spArgs('pwd'), // 从spArgs获取到表单提交上来的title
                                'question' => $this->spArgs('question'),
                                'answer' => $this->spArgs('answer'),
                                'email' => $this->spArgs('email'),
                        );
                        $user->create($newrow);
                        echo "注册成功,返回";
                }
                else
                {
                        echo "用户名已经存在,请换个号码重新注册!";
                }
        }
是不是用spArgs来接收html表单里面的用户名的值。
上面只是用xiaorong321来测试,请问哪里出错了,或者怎么写代码?
谢谢

2011-04-02 09:39:55

#2 jake

1. 用spArgs接收表单值是
$conditions = array( 'user_id' => $this->spArgs('user_id') );

2. 检测是否存在ID的时候,可以是把:

                $result = $test->findCount($conditions); // 使用了findCount
                echo $result;
                if($result==0)
改成

if( !$user->find($conditions)  ){
// 不存在原有用户ID
}else{
// 存在ID
}

另外,注意一下:$result = $test->findCount($conditions);  这句使用的应该是 $user 而不是 $test

2011-04-02 10:15:14

#3 xiaorong321

回复 2 jake

谢谢:)

2011-04-02 10:37:34