为什么MySQLI INSERT不起作用?

时间:2013-06-20 12:14:05

标签: php mysql mysqli

我的MySQLI INSERT查询无效。

在查询中没有任何内容传递给数据库。

其他一切都很好用,所以我不知道为什么查询不起作用。

这是AJAX调用的全部内容,我知道正确接收数据,并且成功返回。

PHP

//Get all POST data from user trying to register
$username = $_POST['username'];
$password =  $_POST['password'];
$name = $_POST['name'];
$email = $_POST['email'];
$lat = $_POST['lat'];
$lon = $_POST['lon'];

//Choose wether the user will be "it" or "not it"
$choose_it = array('it', 'not it');
$key = array_rand($choose_it);
$it = $choose_it[$key];

$message = "";

$allEmails = "";
$allUnames = "";

//Get emails from registered users and check if it has already been used
$AllPeople = "SELECT email, uname FROM login WHERE email = '$email'";
$query = mysqli_query($conn,$AllPeople);

while($row = mysqli_fetch_array($query))
{
    $allEmails = $row['email'];
    $allUnames = $row['uname'];
}

if($allEmails != "")
{
    $message = "That email already exists. You can only have one account per email address.";
}

if($allUnames != "")
{
    $message = "That username has already been taken. Please choose a different one.";
}

if($allEmails === "" && $allUnames === "")
{
    $cost = 10;
    $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
    $salt = sprintf("$2a$%02d$", $cost) . $salt;
    $hash = crypt($password, $salt);

    mysqli_query($conn, "INSERT INTO login (name, uname, pword, email, lat, lon, it) VALUES ($name, $username, $hash, $email, $lat, $lon, $it");

    $message = "goodtogo";
}

echo json_encode(array("message" => $message, "name" => $name, "it" => $it));

mysqli_close($conn);
?>

2 个答案:

答案 0 :(得分:1)

1)你忘记了字符串数据的单引号。

2)VALUES的结束括号

mysqli_query($conn, "INSERT INTO login (name, uname, pword, email, lat, lon, it) 
VALUES ('$name', '$username', '$hash', '$email', '$lat', '$lon', '$it')");

答案 1 :(得分:0)

mysqli_query($conn, "INSERT INTO login (name, uname, pword, email, lat, lon, it) VALUES ($name, $username, $hash, $email, $lat, $lon, $it)");

----> (“.....(....)”);