Mysqli不在数据库中插入记录

时间:2013-12-27 10:31:40

标签: php mysqli

今天我决定将我的项目从mysql更改为mysqli,并在注册页面中不在我的数据库中插入值!

我与数据库文件的连接包含:

$host = 'localhost';
$user = 'demo';
$password = 'demo';
$database = 'demo';
$mysqli = new mysqli($host, $user, $password, $database);
if (mysqli_connect_errno()) {
    exit('Connect failed: '. mysqli_connect_error());
}

这是我的php代码:

<?php            
  if(isset($_POST['submit'])) {

    if(empty($_POST['username']) OR empty($_POST['password']) OR empty($_POST['password2']) OR empty($_POST['email'])) {
    echo "<div class=\"alert-box error\"><span>error: </span>All fields it's required!</div></br>";
    } else {

        $username = $_POST['username'];
        $username_safe = $mysqli->real_escape_string($username);
        $email = $_POST['email'];
        $email_safe = $mysqli->real_escape_string($email);  

       $q1 = "SELECT * FROM user WHERE username ='$username'";
       $result1 = mysqli_query($mysqli,$q1) or die(mysqli_error());
       $result2 = mysqli_num_rows($result1);

       $q2 = "SELECT * FROM user WHERE email ='$email'";
       $result3 = mysqli_query($mysqli,$q2) or die(mysqli_error());
       $result3 = mysqli_num_rows($result3);


        if(!preg_match('#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#is', $email_safe)) {
            echo "<div class=\"alert-box error\"><span>error: </span>Your email address is invalid! Please try again!</div><br />";
        } if($result2 > 0) {
            echo "<div class=\"alert-box error\"><span>error: </span>Username $username it's allready registered! Please insert another!</div><br />";
        } if($result3 > 0) {
            echo "<div class=\"alert-box error\"><span>error: </span>Email $username it's exist in our database!</div><br />";
        } if(!preg_match("#^[a-z0-9]+$#",$username_safe)) {
            echo "<div class=\"alert-box error\"><span>error: </span>Your username is invalid! Please try again!</div><br />";
         if(strlen($password_safe) < 5 OR strlen($password_safe) > 25) {
            echo "<div class=\"alert-box error\"><span>error: </span>Password must be more than 5 and less than 25 characteres!</div><br />";
        } if(strlen($username_safe) < 5 OR strlen($username_safe) > 25) {
            echo "<div class=\"alert-box error\"><span>error: </span>Username must be more than 5 and less than 25 characteres!</div><br />";
        } else {
            $now = date('U');
            $password = md5($password_safe);

     // insert the new record into the database
          $register_sql = "INSERT INTO user (username, password, email) VALUES ('".$username_safe."', '".$password."', '".$email_safe."')";
          $register_res = mysqli_query($mysqli, $register_sql) or die(mysqli_error($mysqli));
      }
    }
  }
}

?>

提交后没有告诉我任何错误!

1 个答案:

答案 0 :(得分:0)

请尝试此代码,请按照面向对象的样式查询$ mysqli-&gt; query()。

[参考面向对象的样式和程序样式http://www.php.net/manual/en/mysqli.query.php,按照任何一种方式进行操作。 ]

 <?php

 $host = 'localhost';
 $user = 'tem001-root';
 $password = 'txrzy9h';
 $database = 'tem001_dev';
 $mysqli = new mysqli($host, $user, $password, $database);
 if (mysqli_connect_errno()) {
     exit('Connect failed: '. mysqli_connect_error());
 } else {
     echo "Connected";
 }



 if(isset($_POST['submit'])) {

if(empty($_POST['username']) OR empty($_POST['password']) OR empty($_POST['password2']) OR empty($_POST['email'])) {
echo "<div class=\"alert-box error\"><span>error: </span>All fields it's required!</div></br>";
} else {

    $username = $_POST['username'];
    $username_safe = $mysqli->real_escape_string($username);
    $email = $_POST['email'];
    $email_safe = $mysqli->real_escape_string($email);  

   $q1 = "SELECT * FROM user WHERE username ='$username'";
   $result1 = $mysqli->query($q1) or die($mysqli->error);
   $result2 = $result1->num_rows;
   //$result1->close();

   $q2 = "SELECT * FROM user WHERE email ='$email'";
   $result3 = $mysqli->query($q2) or die($mysqli->error);
   $result4 = $result3->num_rows;
   //$result3->close();

   $password_safe = $mysqli->real_escape_string($_POST['password']);  

    if(!preg_match('#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#is', $email_safe)) {
        echo "<div class=\"alert-box error\"><span>error: </span>Your email address is invalid! Please try again!</div><br />";
    } 

    if($result2 > 0) {
        echo "<div class=\"alert-box error\"><span>error: </span>Username $username it's allready registered! Please insert another!</div><br />";
    } 

    if($result4 > 0) {
        echo "<div class=\"alert-box error\"><span>error: </span>Email $email it's exist in our database!</div><br />";
    } 

    if(!preg_match("#^[a-z0-9]+$#",$username_safe)) {
        echo "<div class=\"alert-box error\"><span>error: </span>Your username is invalid! Please try again!</div><br />";
    }
    if(strlen($password_safe) < 5 OR strlen($password_safe) > 25) {
        echo "<div class=\"alert-box error\"><span>error: </span>Password must be more than 5 and less than 25 characteres!</div><br />";
    } if(strlen($username_safe) < 5 OR strlen($username_safe) > 25) {
        echo "<div class=\"alert-box error\"><span>error: </span>Username must be more than 5 and less than 25 characteres!</div><br />";
    } else {

       $now = date('U');
       $password = md5($password_safe);

 //insert the new record into the database
      $register_sql = "INSERT INTO user (username, password, email) VALUES ('".$username_safe."', '".$password."', '".$email_safe."')";
      $register_res = $mysqli->query($register_sql) or die($mysqli->error);
  }

} } ?&GT;