PHP表单没有提交数据,只是重新加载页面

时间:2015-05-12 21:32:14

标签: php mysql pdo

我尝试提交此表单,但它只是使用$ Query代码上方的HTML重新加载页面。

inc / common.php是PDO数据库连接脚本,而inc / header.php只是我存储样式表的地方。

<?php 
ob_start();

require('inc/common.php'); ?>
<?php include_once "inc/header.php"; ?>
 <body class="register-page">
<div class="register-box">
  <div class="register-logo">
    <a href="../../index2.html">Our<b>Pet</b>.net</a>
  </div>
  <div class="register-box-body">
    <p class="login-box-msg">Register a new membership</p>
<?php   
    $query = " 
        SELECT 
            signupdisabled 
        FROM settings 
    "; 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    $row = $stmt->fetch(); 
    if($row['signupdisabled'] == 1) 
    { 
        ?>
        <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Registrations are currently disabled by Administrators</a>
            </div>
        </div><?php
    }
    else
    {
    if(!empty($_POST)) 
    { 
        if(empty($_POST['username'])) 
        { ?>
                            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Please enter a username.</a>
            </div>
            </div> 
        <?php } die() ?>
         <?php 
        if(empty($_POST['password'])) 
        { ?>
                            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Please enter a password.</a>
            </div>
            </div> 
        <?php } die() ?>
        <?php 


        if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
        { ?>
            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Invalid eMail Address. Please Try Again.</a>
            </div>
            </div>

        <?php } die() ?>
<?php
        $query = " 
            SELECT 
                1 
            FROM users 
            WHERE 
                username = :username 
        "; 

        $query_params = array( 
            ':username' => $_POST['username'] 
        ); 

        try 
        { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 

            die("Failed to run query: " . $ex->getMessage()); 
        } 


        $row = $stmt->fetch(); 
        if($row) 
        { ?>
            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">This username is already in use.</a>
            </div>
            </div>
        <?php } die() ?>
        <?php 
        $query = " 
            SELECT 
                1 
            FROM users 
            WHERE 
                email = :email 
        "; 

        $query_params = array( 
            ':email' => $_POST['email'] 
        ); 

        try 
        { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 
            die("Failed to run query: " . $ex->getMessage()); 
        } 

        $row = $stmt->fetch(); 

        if($row) 
        { 
            die("This email address is already registered"); 
        } 

        $query = " 
            INSERT INTO 'users' ( 
                username, 
                fullname, 
                password, 
                salt, 
                email 
            ) VALUES ( 
                :username, 
                :fullname, 
                :password, 
                :salt, 
                :email 
            ) 
        "; 


        $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 


        $password = hash('sha256', $_POST['password'] . $salt); 

        for($round = 0; $round < 65536; $round++) 
        { 
            $password = hash('sha256', $password . $salt); 
        } 

        $query_params = array( 
            ':username' => $_POST['username'],
            ':fullname' => $_POST['fullname'], 
            ':password' => $password, 
            ':salt' => $salt, 
            ':email' => $_POST['email']     
        ); 

        try 
        { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 
            die("Failed to run query: " . $ex->getMessage()); 
        } 

        header("Location: login.php"); 

        die("Redirecting to login.php"); 

    } 
    ?> 
    <form role="form" action="register.php" method="post">
      <div class="form-group has-feedback">
        <input class="form-control" type="text" class="form-control" name="username" placeholder="Username"/>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input class="form-control" type="text" class="form-control" name="fullname" placeholder="Full Name"/>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>          
      <div class="form-group has-feedback">
        <input class="form-control" type="email" class="form-control" name="email" placeholder="Email" required/>
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input class="form-control" type="password" class="form-control" name="password" placeholder="Password" required/>
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-8">    
          <div class="checkbox icheck">
            <label>
              <input type="checkbox"> I agree to the <a href="#">terms</a>
            </label>
          </div>                        
        </div><!-- /.col -->
        <div class="col-xs-4">
          <button class="btn btn-primary btn-block btn-flat" type="submit">Register</button>
        </div><!-- /.col -->
      </div>
    </form>        

    <a href="login.html" class="text-center">I already have a membership</a>
  </div><!-- /.form-box -->
</div><!-- /.register-box -->
    <?php } ?>

<?php include_once "inc/footer-lr.php"; ?>

有人可以帮我吗?

2 个答案:

答案 0 :(得分:4)

您为表名使用了不正确的SVG 1.1 spec

INSERT INTO 'users' 
            ^     ^

是常规报价。

删除它们

INSERT INTO users

或使用刻度

INSERT INTO `users`
  • 使用setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)会发出语法错误信号。

在打开连接后立即添加$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

请教:

标识符限定符:

https://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告应仅在暂存时完成,而不是生产。

答案 1 :(得分:0)

Sup man,我为你吸取了两件事。

第一个,就像Egzonr说的那样,尝试组织一下你的代码,我的意思是,你不需要创建分离的文件。但要尽量保持清洁。

另一件事是,创建另一个文件(只是为了测试,之后你会删除它),并尝试运行你的代码,但只是你的表单(删除所有其余的)。如果它工作,你将添加你的脚本的其他部分

我希望这对你有用。 感谢