MySQLi:$MySQLi->;查询将不会插入,并且不会返回任何错误


MySQLi: $mysqli->query will not insert, and no error is returned

我试图通过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->query和prepared语句都不起作用。

以下是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

最后一件事:我在另一个文件中使用了一个非常相似的insert语句

提前感谢

不要使用单引号,而是使用`likes-this:

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

字段名称周围不应该有单引号。我在phpMyAdmin中进行了测试,正如您的示例所示,它失败了,但上面的示例起到了作用。