dongping1689 2014-04-12 15:39
浏览 44
已采纳

php中唯一的用户ID代码不起作用

Recently i was searching for unique username registration using php.. I came across a piece of code which i am displaying below:

<?php 
$fname=trim($_POST['fname']);
$lname=trim($_POST['lname']);
$email=trim($_POST['email']);
$usn=trim($_POST['usn']);
$dept=trim($_POST['dept']);
$pass=trim($_POST['pass']);
$tel=trim($_POST['tel']);

$dbh = mysql_connect('localhost', 'root','') or die("<h3 style=\"color:red;\" align=\"center\">SERVER ERROR</h3>");
mysql_select_db('fy') or die("<h3 style=\"color:red;\" align=\"center\">SERVER ERROR</h3>");

$error= mysql_query("SELECT * FROM stud WHERE email='$email' OR usn='$usn' OR tel='$tel'") or die (mysql_error()); 

if (mysql_num_rows($error) > 0);
{
    die ("Sorry! Either email, usn or tel already exists!");
}

$query="INSERT INTO stud (fname, lname, email, tel, usn, dept, pass) VALUES ('$fname', '$lname', '$email', '$tel', '$usn', '$dept', '$pass')";
mysql_query($query);
$query="INSERT INTO log VALUES ('$usn','$pass',0,0)";
mysql_query($query);
print("REGISTERED");



 ?>
 <a href="login.php">LOGIN</a><br />

At this moment my database is completely empty. I've just created the database stud with the desired columns. Now the problem is when i try to register using my registration page, it gives me the error i specified in die that is

"Sorry! Either email, usn or tel already exists!"

How is this possible if there are no values in the database. In the registration form I've given

action="register.php" 

as a processing file. Also I've tried with mysql_fetch_assoc(), but i get the same error. Any help is appreciated. Thank you .

  • 写回答

1条回答 默认 最新

  • doris20141022 2014-04-12 15:43
    关注

    Your first problem is that, as John Conde states, your code is vulnerable to SQL injection attacks.

    Your second problem, and to answer your question, is probably because you have this:

    if (mysql_num_rows($error) > 0);

    instead of this:

    if (mysql_num_rows($error) > 0)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器