简体   繁体   English

我的 php 没有将值输入到 sql 数据库中,我的代码有什么问题?

[英]My php is not entering the values into the sql database, what is wrong with my code?

I'm having this problem where I have a registration form and I am using PHP and MySQL.我有一个注册表并且使用 PHP 和 MySQL 时遇到了这个问题。 The problem is that even when all the data is valid it wont enter the information into the database.问题是即使所有数据都有效,它也不会将信息输入数据库。 I know the database is connected because I can use it with the login part of my website.我知道数据库已连接,因为我可以将它与我网站的登录部分一起使用。 I think it is the problem with the email and username cross check against the database but I am not sure.我认为这是针对数据库的电子邮件和用户名交叉检查的问题,但我不确定。 Is the positioning of the curly braces or alot more complex?花括号的位置是更复杂还是更复杂?

<?php
  include_once('db.php');

  $name = mysql_real_escape_string( $_POST["name"] );
  $username = mysql_real_escape_string( ($_POST["username"]) );
  $password = mysql_real_escape_string( md5 ($_POST["password"]) );
  $repeatpassword = mysql_real_escape_string( $_POST['repeatpassword'] );
  $email = mysql_real_escape_string( $_POST["email"] );
  $confirmemail = mysql_real_escape_string( $_POST['confirmemail'] );

  // the below if statement is for when the user does NOT have JS enabled in            their browser
  if(empty($name) || empty($username) || empty($password) || empty($email)){
    echo "(*) indicate that the fields are mandatory.";
    exit();
  }

  if($email == $confirmemail){
    exit();
  }else{
    echo "Your Email address does not match.";
  }

  if($email == $repeatpassword){
    exit();
  }else{
    echo "Your Passwords do not match.";
    exit();
}

  $res = mysql_query("SELECT username FROM users WHERE username='$username'");
  $row = mysql_fetch_row($res);
  $res1 = mysql_query("SELECT email FROM users WHERE email='$email'");
  $row1 = mysql_fetch_row($res1);

  if( $row > 0 ){
    echo nl2br("The username $username is already in use");
  }else{

        if( $row1 > 0 ){
            echo nl2br("the email address $email is already in use");
        }else{

        $sql = "INSERT INTO users VALUES('','$name',  '$username', '$password', '$email')";

        if( mysql_query($sql) ){
            echo "Inserted Successfully";
        }else{
        echo "Insertion Failed";
        }
    }  
}

?>
if($email == $confirmemail) {
    exit();
}
else {
    echo "Your Email address does not match.";
}

So what you're doing in the above code is "if email and confirmation email are the same, stop the script execution else print out 'Your Email address does not match.'因此,您在上面的代码中所做的是“如果电子邮件和确认电子邮件相同,则停止脚本执行,否则打印出'您的电子邮件地址不匹配。' and continue execution".并继续执行”。

if  ($email == $repeatpassword) {
  exit();
}
else {
  echo "Your Passwords do not match.";
  exit();
}

And here you are saying if "email and repeatpassword are the same (???), stop script execution else print out 'Your Passwords do not match.'在这里你是说如果“电子邮件和重复密码相同(???),停止脚本执行,否则打印出'你的密码不匹配。' and also stop script execution".并停止脚本执行”。

So because of this logic obviously you never reach the code to insert data to database.因此,显然由于这种逻辑,您永远不会到达将数据插入数据库的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM