mysql防止用户名重复,我似乎无法防止姓名重复输入

Okay, so I've been going at it for a while now.

What I'm trying to achieve is to prevent people from being able to create the same name that is entered in the name='name' field. Here's the html code.

Name Your Card

and this is my mysql page.

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "christmas";

// Create connection

$dbhandle = mysqli_connect ($servername, $username, $password, $dbname) or die ("could not connect to database");

$selected = mysql_connect('christmas', $dbhandle);

$query = mysql_query("SELECT * FROM list WHERE name='$name'");

if(mysql_num_rows($query) > 0){

echo 'that name already exists';

}else{

mysql_query("INSERT INTO list(name, one , two, three, four, five, six, seven, eight) VALUES ('$name' , '$one' , '$two' , '$three' , '$four' , '$five' , '$six', '$seven' , '$eight')");

}

mysql_close();

?>

what the heck am I doing wrong?

解决方案

There is quite a few things wrong here.

You're using a MySQL keyword list as a function, and that alone will cause you problems.

Sidenote: Yes, I know it's not a "reserved" word, it's a "keyword" and MySQL treats keywords specially if used as a function, which is what you're doing now in your second query, and is the way that MySQL is interpreting it as; a function rather than a table declaration name.

I suggest you rename that table to lists, or wrap it in ticks.

You're also mixing MySQL APIs/functions that do not intermix.

So your new code would read as,

while dropping $selected = mysql_connect('christmas', $dbhandle);

$query = mysqli_query($dbhandle, "SELECT * FROM `list` WHERE name='$name'")

or die (mysqli_error($dbhandle));

if(mysqli_num_rows($query) > 0){

echo 'that name already exists';

}else{

mysqli_query($dbhandle, "INSERT INTO `list` (name, one, two, three, four, five, six, seven, eight)

VALUES ('$name' , '$one' , '$two' , '$three' , '$four' , '$five' , '$six', '$seven' , '$eight')")

or die (mysqli_error($dbhandle));

}

mysqli_close($dbhandle);

Your code is also open to an SQL injection. Use a prepared statement.

Now, we have no idea as to where all your variables are coming from.

Use error reporting.

I also suggest you use a conditional empty() for your variables/inputs.

Otherwise, you may also get other errors that MySQL may complain about.

I.e.:

if(!empty($var)){...}

Ultimately and to make sure there are no duplicate entries in your database, you can set a UNIQUE constraint.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 注册接口 ```javascript app.post('/register', (req, res) => { const username = req.body.username; const password = req.body.password; const sql = 'SELECT * FROM users WHERE username = ?'; conn.query(sql, [username], (err, results) => { if (err) { throw err; } if (results.length > 0) { // 如果该用户名已存在 res.json({ code: 1, message: '该用户名已被注册' }); } else { // 否则,进行注册 const insertSql = 'INSERT INTO users(username, password) VALUES(?, ?)'; conn.query(insertSql, [username, password], (err, result) => { if (err) { throw err; } res.json({ code: 0, message: '注册成功' }); }); } }); }); ``` 2. 登录接口 ```javascript app.post('/login', (req, res) => { const username = req.body.username; const password = req.body.password; const sql = 'SELECT * FROM users WHERE username = ? AND password = ?'; conn.query(sql, [username, password], (err, results) => { if (err) { throw err; } if (results.length > 0) { // 如果登录成功 res.json({ code: 0, message: '登录成功' }); } else { // 否则,登录失败 res.json({ code: 1, message: '用户名或密码错误' }); } }); }); ``` 3. 校验用户名重复接口 ```javascript app.get('/checkUsername', (req, res) => { const username = req.query.username; const sql = 'SELECT * FROM users WHERE username = ?'; conn.query(sql, [username], (err, results) => { if (err) { throw err; } if (results.length > 0) { // 如果该用户名已存在 res.json({ code: 1, message: '该用户名已被注册' }); } else { // 否则,用户名可用 res.json({ code: 0, message: '用户名可用' }); } }); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值