PHP:注册表单不发送到mySQL数据库

时间:2017-03-14 04:33:29

标签: php mysql forms

我收到了一份当前没有发送数据的注册表单,或者该功能未按预期工作。

首页获得了必须使用POST方法发送到sign-up.php的所有表单,所有用户输入都将被条带化,如果所有请求表单数据被用户推送,将被发送到方法{{1在register类中。 该类处理mysql提交。

数据库连接按预期工作。

HTML表单

USER

登录-up.php

<form id="signupform" class="form-horizontal" role="form" action="sign-up.php" method="POST">

   <div id="signupalert" style="display:none" class="alert alert-danger">
      <p>Errore:</p>
   </div>
   <div class="form-group">
       <label for="email" class="col-md-3 control-label">Email</label>
       <div class="col-md-9">
           <input type="text" class="form-control" name="email" placeholder="Indirizzo e-Mail">
       </div>
   </div>
   <div class="form-group">
       <label for="username" class="col-md-3 control-label">Username</label>
       <div class="col-md-9">
           <input type="text" class="form-control" name="username" placeholder="Username">
       </div>
   </div>
   [ ... Striped Code ...]                         
   <div class="col-md-offset-3 col-md-9">
        <input type="submit" name="submit" id="submit" type="button" class="btn btn-info" value="Registrati"/>
        <button id="btn-fbsignup" type="button" class="btn btn-primary"><i class="glyphicon glyphicon-facebook"></i>Registrati con Facebook</button>
   </div>
</form>

class.user.php

<?php
session_start();
require_once('core/class.user.php');
$user = new USER();
var_dump($_POST);
if($user->is_loggedin()!="")
{
    $user->redirect('home.php');
}

if(isset($_POST['submit']))
{
    $uname       = strip_tags($_POST['username']);
    $umail       = strip_tags($_POST['email']);
    $upass       = strip_tags($_POST['password']);
    $firstname   = strip_tags($_POST['firstname']);
    $lastname    = strip_tags($_POST['lastname']);
    $address     = strip_tags($_POST['address']);
    $cap         = strip_tags($_POST['cap']);
    $city        = strip_tags($_POST['city']);
    $prov        = strip_tags($_POST['prov']);
    $tel         = strip_tags($_POST['tel']);

    if($uname=="")   {
        $error[] = "provide username !";
    }
    else if($umail=="")  {
        $error[] = "provide email id !";
    }
    else if(!filter_var($umail, FILTER_VALIDATE_EMAIL))  {
        $error[] = 'Please enter a valid email address !';
    }
    else if($upass=="")  {
        $error[] = "provide password !";
    }
    else if(strlen($upass) < 6){
        $error[] = "Password must be atleast 6 characters";
    }
    else
    {
        try
        {
            $stmt = $user->runQuery("SELECT user_name, user_email FROM users WHERE user_name=:uname OR user_email=:umail");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $row=$stmt->fetch(PDO::FETCH_ASSOC);

            if($row['user_name']==$uname) {
                $error[] = "sorry username already taken !";
            }
            else if($row['user_email']==$umail) {
                $error[] = "sorry email id already taken !";
            }
            else
            {
                if($user->register($uname,$umail,$upass,$firstname,$lastname,$address,$cap,$city,$prov,$tel)){
                    $user->redirect('sign-up.php?joined');
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }
}
if ($_GET['joined']) {
    print 'Registrazione completata';
}
?>

dbconfig.php

class USER
{

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function register($uname, $umail, $upass, $firstname, $lastname, $address, $cap, $city, $prov, $tel)
    {
        try {

            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass,disk_quota, details_nome, details_cognome, details_indirizzo, details_cap, details_citta, details_provincia, details_telefono) 
                                                   VALUES(:uname, :umail, :upass, '209715200', :firstname, :lastname, :address, :cap, :city, :prov, :tel)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);
            $stmt->bindparam(":firstname", $firstname);
            $stmt->bindparam(":lastname", $lastname);
            $stmt->bindparam(":address", $address);
            $stmt->bindparam(":cap", $cap);
            $stmt->bindparam(":city", $city);
            $stmt->bindparam(":prov", $prov);
            $stmt->bindparam(":tel", $tel);

            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    [... striped rest of class code ...]
    public function is_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
           return true;
        }
    }
  }
  [... striped rest of class code ...]

MySQL日志

MySQL日志仅返回与数据库的初始连接

<?php
class Database
{   
   private $host = "localhost";
   private $db_name = "c9";
   private $username = "andreaem_dev";
   private $password = "";
   public $conn;

   public function dbConnection()
   {

       $this->conn = null;    
       try
       {
           $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8", $this->username, $this->password);
           $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
      }
   }

POST数据的var_dump()

170314  4:09:26   163 Connect   andreaem_dev@localhost on c9
163 Quit    

行为 当表单提交到/home/ubuntu/workspace/auth/sign-up.php:5: array(12) { 'email' => string(19) "johndoe@example.com" 'username' => string(7) "johndoe" 'firstname' => string(4) "John" 'lastname' => string(3) "Doe" 'address' => string(15) "Fantasy Road,14" 'cap' => string(6) "010101" 'city' => string(4) "Roma" 'prov' => string(4) "Roma" 'tel' => string(13) "+395551234567" 'passwd' => string(8) "jdoe1234" 'repasswd' => string(8) "jdoe1234" 'submit' => string(10) "Registrati" } 时,除了sign_up.php打印输出外,我都有一个空白页。没有插入数据库并且没有显示错误

我希望我已经把所有需要放在了解问题所在的位置。

2 个答案:

答案 0 :(得分:1)

感谢CodeGodie的帮助,我发现问题出在位于sign_up.php页面的IF块中,删除了代码按预期工作并创建了用户。

我将使用更加用户友好的jQuery来管理它。 感谢

答案 1 :(得分:0)

此声明if($user->is_loggedin()!="")中有错误,因为true!=""如果用户已登录,则为真,那么您将被重定向到home.php,将其替换为:{/ p} >

if(!$user->is_loggedin())