3

我正在尝试通过 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 和准备好的语句都不起作用。

这是 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

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

提前致谢

4

1 回答 1

1

不要使用单引号,而是使用 ` 喜欢这样:

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

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

于 2012-08-26T14:42:00.510 回答