数据库记录未创建?

时间:2015-08-24 01:25:23

标签: php

我有以下代码,它应该添加一个它说它正在做的数据库值,但由于某种原因,没有创建数据库记录。

每个$ values []数组项都有一个值。我得到的信息是" echo"消息:"用户已成功创建,请在登录之前确认您的电子邮件"。

  

(显然没有完成更好的验证将完成和   加密和盐渍添加等)

以下是代码:

<?php
//Login Script
require("dbconnect.php");
require("functions.php");

$message = array();

//Check values
$values = array("fname" => clean($_POST['fname']),
                "sname" => clean($_POST['sname']),
                "email" => clean($_POST['email']),
                "reg" => clean($_POST['reg']),
                "pass" => clean($_POST['password']),
                "repass" => clean($_POST['repassword']));

$error = true; //If all tests pass there will be no error.
//Check that there is a value in field
$checks = false;
if (strlen($values['fname']) > 2) {
    if (strlen($values['sname']) > 2) {
        if (strlen($values['email']) > 2) {
              if (strlen($values['pass']) > 2) {
                  if ($values['repass'] == $values['pass']) {
                      //All tests pass
                      $error = false;
                  } else {
                      //Passwords don't match
                      exit("Passwords do not match");
                  }
              } else {
                  //No password entered
                  exit("You must enter a password");
              }
        } else {
            //No email entered
            exit("Please enter a valid email address");
        }
    } else {
        //No second name entered
        exit("Please enter your second name");
    }
} else {
    //No first name entered
    exit("Please enter your first name");
}

//Encrypt password and generate salt
$values['salt'] = generateRandomString(32);

if ($error == false) {
    $user_exists = true; //Check if use exists
    if ($userExist = $conn->prepare("SELECT id FROM users WHERE email=?")) {
        $userExist->bind_param('s', $email);
        $userExist->execute();
        $userExist->store_result();
        $numRows = $userExist->num_rows;

        //If user exists don't create another one
        if ($numRows < 1) {
            //Connection already established, prepare bind.
            if ($stmt = $conn->prepare("INSERT INTO users (firstname,secondname,email,password,reg,salt) VALUES (?,?,?,?,?,?)")) {
                $stmt->bind_param("ssssss",
                                $values['fname'],
                                $values['sname'],
                                $values['email'],
                                $values['password'],
                                $values['reg'],
                                $values['salt']);
                $stmt->execute(); //Add user
            } else {
                //Error creating user
                printf("Error message: %s\n", $conn->error);
            }
            //Close the connections
            $stmt->close();
            $conn->close();

            //User created
            echo "User successfully created, please confirm your email before you can login";
        } else {
            echo "User already exists with entered email";
        }
        //Close connection
        $userExist->close();
    } else {
        //Error checking if user exists
        printf("Error message: %s\n", $conn->error);    
    }
}
?>

1 个答案:

答案 0 :(得分:0)

我的愚蠢错字,我把$ values ['password']代替$ values ['pass']!忘了检查execute()是否已正确执行,我现在也做了,谢谢你们!