MySQLi:$ mysqli->查询不会插入,也不会返回错误

时间:2012-08-26 14:19:55

标签: php mysql mysqli

我试图通过MySQLi将简单数据插入到我的MySQL表中,但是它拒绝插入,没有报告错误消息。

我想强调一下,当直接输入PhpMyAdmin时,此查询功能正常(当然,替换了变量)

<?php
session_start();
require_once('recaptchalib.php');
require_once('./inc/crypt.php');
require_once('./inc/mysql.php');

$privatekey = "-------------------------------- ";

$u = $mysqli->real_escape_string($_POST['username']);
$p = crypto($_POST['password']);
$e = $mysqli->real_escape_string($_POST['email']);

$resp = recaptcha_check_answer ($privatekey,
    $_SERVER["REMOTE_ADDR"],
    $_POST["recaptcha_challenge_field"],
    $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
      $_SESSION['loginError'] = "The captcha was entered incorrectly!";
      header("LOCATION: ./index.php?pg=1");
      die();
  }


$amt = 0;

if($data = $mysqli->prepare("SELECT * FROM users WHERE username=? OR email=?")){
    $data->bind_param("ss", $u, $e);
    $data->execute();
    $data->store_result();
    $amt = $data->num_rows();
    $data->close();
}
if($amt != 0){
    $_SESSION['loginError'] = "A user has already registered with either that email or username.";
    header("LOCATION: ./index.php?pg=1");
    die();
}

if(strlen($u) < 5){
    $_SESSION['loginError'] = "A username must be greater than 5 characters long.";
    header("LOCATION: ./index.php?pg=1");
    die();
}

if(strlen($e) <= 5){
    $_SESSION['loginError'] = "An email must be greater than 5 characters long.";
    header("LOCATION: ./index.php?pg=1");
    die();
}

if(!strstr($e, "@")){
    $_SESSION['loginError'] = "A valid email must be entered.";
    header("LOCATION: ./index.php?pg=1");
    die();
}

if(!ctype_alnum($u)){
    $_SESSION['loginError'] = "Your username contains an invalid character.";
    header("LOCATION: ./index.php?pg=1");
    die();
}
$ip = $_SERVER['REMOTE_ADDR']."r";

$mysqli->query("INSERT INTO users (`username`, `password`, `email`, `perms`, `ip`) VALUES ('".$u."', '".$p."', '".$e."', '0', '".$ip."')");


header("LOCATION: ./index.php?win=1");

?>

我还应该注意,同时使用$ mysqli-&gt;查询和预处理语句都不起作用。

这是MySQLi部分下的我的phpinfo():

Client API library version  5.0.95
Client API header version   5.0.91
MYSQLI_SOCKET   /var/lib/mysql/mysql.sock

Directive   Local Value Master Value
mysqli.default_host no value    no value
mysqli.default_port 3306    3306
mysqli.default_pw   no value    no value
mysqli.default_socket   no value    no value
mysqli.default_user no value    no value
mysqli.max_links    Unlimited   Unlimited
mysqli.reconnect    Off Off

最后一件事:我在一个不同的文件中使用了非常相似的插入语句。

提前致谢

1 个答案:

答案 0 :(得分:1)

不要使用单引号而是使用`赞这个:

$mysqli->query("INSERT INTO `users` (`username`, `password`, `email`, `perms`, `ip`) VALUES ('".$u."', '".$p."', '".$e."', '0', '".$ip."')");

字段名称周围不应该有单引号。我在phpMyAdmin中进行了测试,并且您的示例显示它失败了,但上面的示例有效。