2015-12-03 2 views
0

Когда я исполняю заполнить поля и отправить это дает мне> { «ошибка»: правда, «error_msg»: «Неизвестная ошибка произошла в регистрации!»}{"error": true, "error_msg": "Неизвестная ошибка при регистрации!"} Во время исполнения ..!

DB_Functions.php

<?php 
class DB_Functions { 
    private $db; 

    // constructor 
    function __construct() { 
     try { 
      $hostname = "localhost"; 
      $dbname = "miisky"; 
      $dbuser = "root"; 
      $dbpass = ""; 
      $this->db = new PDO("mysql:host=$hostname;dbname=$dbname", $dbuser, $dbpass); 
     } 
     catch(PDOException $e) 
     { 
      echo $e->getMessage(); 
     } 
    } 

    /** 
     * Storing new user 
     * returns user details 
     */ 
    public function storeUser($fname, $lname, $email, $password, $mobile) { 
     try { 
      $hash = md5($password); 
      $sql = "INSERT INTO db_name(fname, lname, email, password, mobile) VALUES ('$fname', '$lname', '$email', '$hash', '$mobile')"; 
      $result = $this->db->query($sql); 

      if ($result) { 
       // get user details 
       $id = $this->db->lastInsertId(); //last inserted id 
       $sql = "SELECT * FROM db_name WHERE email = $email"; 
       $result = $this->db->query($sql); 
       $no_of_rows = $result->fetchColumn(); 
       // returns confirmation message if completed 
       if ($no_of_rows > 0) { 
        return "existsandcompleted"; 
       } 
      } 
     } 
     catch (Exception $e) { 
      $error = 'Error accessing database: ' . $e->getMessage(); 
     } 
    } 
} 
?> 

и здесь фактический код PHP: register.php

<?php 
require_once 'DB_Functions.php'; 
$db = new DB_Functions(); 

// json response array 
$response = array("error" => FALSE); 
if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['mobile'])) { 
    // receiving the post params 
    $fname = $_POST['fname']; 
    $lname = $_POST['lname']; 
    $email = $_POST['email']; 
    $password = $_POST['password']; 
    $mobile = $_POST['mobile']; 

    // create a new user 
    $user = $db->storeUser($fname, $lname, $email, $password, $mobile); 
    if ($user) { 
     // user stored successfully 
     $response["error"] = FALSE; 
     $response["uid"] = $user["id"]; 
     $response["user"]["fname"] = $user["fname"]; 
     $response["user"]["lname"] = $user["lname"]; 
     $response["user"]["email"] = $user["email"]; 
     $response["user"]["created_at"] = $user["created_at"]; 
     $response["user"]["updated_at"] = $user["updated_at"]; 
     echo json_encode($response); 
    } else { 
     // user failed to store 
     $response["error"] = TRUE; 
     $response["error_msg"] = "Unknown error occurred in registration!"; 
     echo json_encode($response); 
    } 
} else { 
    $response["error"] = TRUE; 
    $response["error_msg"] = "Required parameters (fname, lname, email, password or mobile) is missing!"; 
    echo json_encode($response); 
} 
?> 

и, наконец, вот HTML код: register.html

<html> 
    <form name = "register" action = "register.php" id = "register" method = "POST"> 
     <label>First name:</label> 
     <input type = text name = "fname" id = "fname"> 
     <label>Last name:</label> 
     <input type = "text" name = "lname" id = "lname"> 
     <label>E-mail:</label> 
     <input type = "email" name = "email" id = "email"> 
     <label>Password</label> 
     <input type = "password" name = "password" id = "password"> 
     <label>Mobile no:</label> 
     <input type = "number" name = "mobile" id = "mobile"> 
     <input type="submit" value="Insert" name="submit" id="submit" href = "#"> 
    </form> 
</html> 

ответ

0

Вы ошибка в вашем запросе на выборке, так ваша функция терпит неудачу и не возвращает ничего

Вы должны изменить

$sql = "SELECT * FROM db_name WHERE email = $email";// need quotes here 

Для

$sql = "SELECT * FROM db_name WHERE email = '" . $email . "'"; 
$result = $this->db->query($sql); 
$no_of_rows = $result->fetchColumn(); 
// returns confirmation message if completed 
if ($no_of_rows > 0) { 
    return "existsandcompleted"; 
} else { 
    return FALSE; 
} 

Измените свою вставку запрос

$sql = "INSERT INTO db_name(fname, lname, email, password, mobile) VALUES ('$fname', '$lname', '$email', '$hash', '$mobile')"; 
$dbh = $this->db->prepare($sql); 
$dbh->execute(); 
$result = $this->db->rowCount(); 

Com Функция plete бы

<?php 

class DB_Functions { 

    private $db; 

// constructor 
    function __construct() { 
     try { 
      $hostname = "localhost"; 
      $dbname = "miisky"; 
      $dbuser = "root"; 
      $dbpass = ""; 
      $this->db = new PDO("mysql:host=$hostname;dbname=$dbname", $dbuser, $dbpass); 
     } catch (PDOException $e) { 
      echo $e->getMessage(); 
     } 
    } 

    /** 
    * Storing new user 
    * returns user details 
    */ 
    public function storeUser($fname, $lname, $email, $password, $mobile) { 
     try { 
      $hash = md5($password); 
      $sql = "INSERT INTO db_name(fname, lname, email, password, mobile) VALUES ('$fname', '$lname', '$email', '$hash', '$mobile')"; 
      $dbh = $this->db->prepare($sql); 
      $dbh->execute(); 
      $result = $this->db->rowCount(); 

      if ($result) { 
// get user details 
       $id = $this->db->lastInsertId(); //last inserted id 
       $sql = "SELECT * FROM db_name WHERE email = '" . $email . "'"; 
       $result = $this->db->query($sql); 
       $no_of_rows = $result->fetchColumn(); 
// returns confirmation message if completed 
       if ($no_of_rows > 0) { 
        return "existsandcompleted"; 
       } else { 
        return FALSE; 
       } 
      } else { 
       return FALSE; 
      } 
     } catch (Exception $e) { 
      $error = 'Error accessing database: ' . $e->getMessage(); 
     } 
    } 

} 

?> 
+0

Yaa ... спасибо за зуб, но код по-прежнему дает мне ту же ошибку, т.е. { «ошибка»: правда, «ERROR_MSG»: «Произошла неизвестная ошибка в регистрации»} во время выполнения .. ! –

+0

Вы можете вставить данные в базу данных ??? – Saty

+0

noo ... это wer проблема есть .. !!! –

Смежные вопросы