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

时间:2016-05-06 15:25:54

标签: php mysql if-statement

我遇到这个问题,我有一个注册表,我正在使用PHP和MySQL。问题是即使所有数据都有效,它也不会将信息输入数据库。我知道数据库已连接,因为我可以将它与我网站的登录部分一起使用。我认为这是针对数据库的电子邮件和用户名交叉检查的问题,但我不确定。花括号的位置是否更复杂?

<?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";
        }
    }  
}

?>

1 个答案:

答案 0 :(得分:0)

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

因此,您在上述代码中执行的操作是&#34;如果电子邮件和确认电子邮件相同,请停止执行脚本,否则请打印出“您的电子邮件地址不匹配。”#39 ;并继续执行&#34;。

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

在这里你要说的是,如果&#34;电子邮件和重复密码是相同的(???),停止脚本执行其他打印输出&#39;你的密码不匹配。&#39;并停止脚本执行&#34;。

因此,由于这种逻辑,您显然无法访问将数据插入数据库的代码。