输入数据库失败

时间:2013-08-21 19:00:28

标签: php database mysqli

我遇到以下代码时出现问题:

$sql= "INSERT INTO Users(Username, Password, Lastname, Email) VALUES
    ('$hash', '$lastname', '$email', '$email')";

  mysqli_query($MyConnection, $sql);

  if(!mysqli_query($MyConnection, $sql)) {
    echo 'We are sorry, there are some problems with saving your data. Please try again within a few minutes.';
  }
  else {
    echo 'We have succesfully saved your data. An activation e-mail will now be send to the e-mail address that you
      have provided us.';
  }

由于错误输入或误用功能,我没有直接错误。然而,我确实得到了失败的if语句的消息,"我们很遗憾(..)"文本。

执行mysqli_query($ MyConnection,$ sql)函数时一定有问题。但我不知道它在哪里。

P.S。我无法发布图片,因为我的声誉低于10.(将其限制在这一点非常奇怪)

正如你们中的一些人提供了大部分/全部代码:

<?php

  // Opens the connection of the MySQL Database
  $MyConnection = mysqli_connect('fdb6.biz.nf', '1446018_amp', '-') 
         or die("Could not connect to the database, please try again");
  mysqli_select_db($MyConnection,'Users');

  mysqli_connect_errno();

  // Website Url:
  $website = 'http://www.askmephilosophy.co.nf/';

  // Information provided by the user
  $username = $_POST['username'];
  $password = $_POST['password'];   // Will get encrypted.
  $lastname = $_POST['lastname'];
  $email = $_POST['email'];

  // A higher "cost" is more secure but consumes more processing power
  $cost = 5;

  // Create a random salt
  $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');

  // Prefix information about the hash so PHP knows how to verify it later.
  // "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
  $salt = sprintf("$2a$%02d$", $cost) . $salt;

  // Hash the password with the salt
  $hash = crypt($password, $salt);

  $sql= "INSERT INTO Users(Username, Password, Lastname, Email) VALUES
    ('$hash', '$lastname', '$email', '$email')";

  mysqli_query($MyConnection, $sql);
  var_dump(mysqli_error($MyConnection));

  if(mysqli_query($MyConnection, $sql)) {
    echo 'We have succesfully saved your data. An activation e-mail will now be send to the e-mail address that you
      have provided us.';
  }
  else {
    echo 'We are sorry, there are some problems with saving your data. Please try again within a few minutes.';
    mysqli_error($MyConnection);
  }

  mysqli_close($MyConnection);

?>

4 个答案:

答案 0 :(得分:3)

$sql= "INSERT INTO Users(Username, Password, Lastname, Email) VALUES
('$hash', '$lastname', '$email')";

这是你的第一个问题;你的桌子有四列,你传三个。此查询保证失败。

mysqli_query($MyConnection, $sql);

if(!mysqli_query($MyConnection, $sql)) {

您正在调用查询函数两次。您只需一次通话即可完成此操作:

if(!mysqli_query($MyConnection, $sql)) {
     // add some error handling code here
     // store the return value of mysqli_error() somewhere
     echo 'We are sorry, there ar....';

由于你使用的是mysqli_,你也应该使用预备语句;我希望至少在尝试将数据库输入添加到数据库之前对数据库输入进行清理。

答案 1 :(得分:0)

为什么您只有3个值,它与您尝试插入的项目数量不匹配(4)......

 $sql= "INSERT INTO Users(Username, Password, Lastname, Email) VALUES
('$username', '$hash', '$lastname', '$email')";

修改

我可能会像这样写

 $sql= "INSERT INTO Users(Username, Password, Lastname, Email) VALUES
    ({$username}, {$hash}, {$lastname}, {$email})";

修改 您的密码不能是' - '

我会像这样更新您的连接信息:

$db = new mysqli('fdb6.biz.nf', 'user', 'pass', 'Users');

if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}

再次编辑:

    $myConnection = new mysqli('fdb6.biz.nf', 'user', 'pass', '1446018_amp');
    $myConnection->mysqli_select_db($MyConnection,'Users'); 

答案 2 :(得分:0)

尝试添加,我想你忘记了这一点。值始终必须等于列

$sql= "INSERT INTO Users(Username, Password, Lastname, Email) VALUES
('$username', '$hash', '$lastname', '$email')";

答案 3 :(得分:0)

首先,您要插入两次记录,因为有两个mysqli_query($MyConnection, $sql);个实例。你可以删除第一个。

这里的问题是你在4个字段中插入3个值。

无论如何,您可以通过

获得特定错误
mysqli_error($MyConnection);

永久地在回声中添加它,或者在新行中添加var_dump(mysqli_error($MyConnection));