用户已被带走?

时间:2014-01-28 09:10:10

标签: php mysql sql

我有这段代码:

$username = $_POST["username"];
$password_input = $_POST["password"];
$password = md5($password_input);
$email_input = $_POST["emailaddress"];
$email = md5($email_input);

if (!($stmt  = $con->prepare("INSERT INTO `users` (`username`,`password`,`email_address`) VALUES (?,?,?)")) || !is_object($stmt)) {
    die( "Error preparing: (" .$con->errno . ") " . $con->error);
}
$stmt->bind_param('sss', $username, $password, $email);
$stmt->execute();
$stmt->close();
echo "User has been Created! Feel free to login - <a href='login.php'><span class='button color_blue'>Login</span></a>";

在SQL数据库中,电子邮件/用户名只能使用一次(UNIQUE),我想知道是否有办法将回显更改为仅在数据成功添加时出现,然后是另一条消息,如果它不是'成功。

谢谢 - 我还是个菜鸟!

编辑:所以在使用了答案中的一些代码后我现在在:

$username = $_POST["username"];
$password_input = $_POST["password"];
$password = md5($password_input);
$email_input = $_POST["emailaddress"];
$email = md5($email_input);

if (!($stmt  = $con->prepare("INSERT INTO `users` (`username`,`password`,`email_address`) VALUES (?,?,?)")) || !is_object($stmt)) {die( "Error preparing: (" .$con->errno . ") " . $con->error);}
$stmt->bind_param('sss', $username, $password, $email);
$stmt->execute();
$stmt->close();
if ($con->affected_rows == 1) {echo "User has been Created! Feel free to login - <a href='login.php'><span class='button color_blue'>Login</span></a>";}
var_dump($con->affected_rows);

成功和不成功的INSERTS由于某种原因都将-1作为“受影响的行”输出

6 个答案:

答案 0 :(得分:1)

如果成功,execute()方法返回true。

替换:

$stmt->execute();

使用:

if($stmt->execute()) { 
    echo "user created!";
} else {
    echo "error: " . $stmt->error;
}

答案 1 :(得分:0)

要检查UPDATE / INSERT / DELETE查询是否成功,您需要检查返回的受影响行是否大于零。

http://php.net/mysqli_affected_rows

  

返回受上一次INSERT,UPDATE影响的行数,   REPLACE或DELETE查询。

在你的情况下:

if ($con->affected_rows == 1) {
    echo "User has been Created! Feel free to login - <a href='login.php'><span class='button color_blue'>Login</span></a>";
}

答案 2 :(得分:0)

是的,您只需检查查询是否成功:

通过以下方式更改代码的结尾:

$success = $stmt->execute();
$stmt->close();
if ($success)
  echo "User has been Created! Feel free to login - <a href='login.php'><span class='button color_blue'>Login</span></a>";
else
  echo "Impossible to create that user: ".$stmt->error;

答案 3 :(得分:0)

提供许多不同的消息,我使用标志。简单地说:如果用户名等于现有的数据库名称,则userExistMsg = 1。 要检查用户是否存在,只需在sql中使用SELECT查询并询问给定用户的任何信息。如果用户名是免费的,则此查询应该导致无对象。

顺便说一下。你似乎使用简单的md5进行pw编码。这不是非常安全。更好地使用盐渍密码之类的东西。

答案 4 :(得分:0)

试试这个。这是根据您的代码更改此示例的答案。

     $username = $_POST["username"];
        $password_input = $_POST["password"];
        $password = md5($password_input);
        $email_input = $_POST["emailaddress"];
        $email = md5($email_input);

        $con=mysqli_connect("HOST","USER","PASSWORD","your_db");
        // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }

        $result = mysqli_query($con,"SELECT COUNT(username) FROM users WHERE username = $username ");

      $datacount2 = mysql_num_rows($result );  
        if($datacount2 < 1)
        {
        if (!($stmt  = $con->prepare("INSERT INTO `users` (`username`,`password`,`email_address`) VALUES (?,?,?)")) || !is_object($stmt)) {
            die( "Error preparing: (" .$con->errno . ") " . $con->error);
        }
        $stmt->bind_param('sss', $username, $password, $email);
        $stmt->execute();
        $stmt->close();

    if($stmt)
     {
        echo "User has been Created! Feel free to login - <a href='login.php'><span class='button color_blue'>Login</span></a>";
      }
      else
     {
     echo "Insert Failed";
     }
        }

        else
        {
        echo "User already exists..";
        }

答案 5 :(得分:-1)

您可以使用mysqli_stmt::affected_rows查看行是否受到影响。如果没有,您可以打印出错误信息