0

我在使用以下代码时遇到问题:

$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)函数的执行肯定有问题。但我看不出它在哪里。

PS我不能发布图片,因为我的声誉低于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

4 回答 4

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_,因此您还应该使用准备好的语句;我希望至少您在尝试将数据库输入添加到数据库之前对其进行了清理。

于 2013-08-21T19:05:41.400 回答
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'); 
于 2013-08-21T19:03:07.583 回答
0

尝试添加,我想你忘记了这个。值总是必须等于列

$sql= "INSERT INTO Users(Username, Password, Lastname, Email) VALUES
('$username', '$hash', '$lastname', '$email')";
于 2013-08-21T19:05:01.250 回答
0

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

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

无论如何,你可以得到具体的错误

mysqli_error($MyConnection);

将它永远添加到您的回声末尾或var_dump(mysqli_error($MyConnection));新行中。

于 2013-08-21T19:06:02.117 回答