douqin2108 2017-03-14 04:33
浏览 50
已采纳

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

I got a registration form that currently not send data or the function isn't work as expected.

First page got all the form that must to be sent to sign-up.php using POST method, all the user input will be striped and if all the request form data is putted by user will be sent to method registerin USER class. The class handle the mysql submission.

The database connection work as expected.

HTML Form

<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>

sign-up.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';
}
?>

class.user.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 ...]

dbconfig.php

<?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;
      }
   }

MySQL log

MySQL log return only the initial connection to database

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

var_dump() of POST data

/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" }

Behavior When the form is submitted to sign_up.phpI got a blank page, except for the var_dump() print-out. Nothing where inserted in database and no errors shown

I hope that I've put all the needed to understand where the problem is.

  • 写回答

2条回答 默认 最新

  • dongwo6477 2017-03-14 05:23
    关注

    Thanks to CodeGodie help, I've found that the problem is in the IFs blocks located at sign_up.php page, removing this the code work as expected and the user is created.

    I will manage that using jQuery that is more user friendly too. Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入