我不知道为什么当我提交时,isset功能不起作用

时间:2017-08-29 05:00:56

标签: php mysql

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php

    $con = mysql_connect('localhost', 'root','');
    mysql_select_db ('product');
    echo "Connected to database";
    session_start();
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Register</title>
<script type="text/javascript" src="assets/js/bootstrap.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
<link rel="stylesheet" href="reg.css" />

        <!-- Google Fonts -->
<link href='https://fonts.googleapis.com/css?family=Passion+One' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Oxygen' rel='stylesheet' type='text/css'>

<script src="jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<table align="center">
<td>
<tr>
    <div align="center" style="size:30%;" class="container">
            <div class="row main" style="width:30%;">
                <div class="panel-heading">
                   <div class="panel-title text-center">
                        <h1 class="title">Register Here</h1>
                        <hr />
                    </div>
                </div> 
                <div class="main-login main-center">
                    <form class="form-horizontal" method="post" action="<?php 
         echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

                        <div align="left" class="form-group">
                            <label name="name" class="cols-sm-2 control-label">Your Name</label>
                            <div class="cols-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
                                    <input type="text" class="form-control" name="name"  placeholder="Enter your Name"/>
                                </div>
                            </div>
                        </div>

                        <div align="left" class="form-group">
                            <label name="email" class="cols-sm-2 control-label">Your Email</label>
                            <div class="cols-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
                                    <input type="text" class="form-control" name="email"   placeholder="Enter your Email"/>
                                </div>
                            </div>
                        </div>

                        <div align="left" class="form-group">
                            <label name="user_name" class="cols-sm-2 control-label">Username</label>
                            <div class="cols-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-users fa" aria-hidden="true"></i></span>
                                    <input type="text" class="form-control" name="user_name"   placeholder="Enter your Username"/>
                                </div>
                            </div>
                        </div>

                        <div align="left" class="form-group">
                            <label name="password" class="cols-sm-2 control-label">Password</label>
                            <div class="cols-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
                                    <input type="password" class="form-control" name="password"   placeholder="Enter your Password"/>
                                </div>
                            </div>
                        </div>
                        <div align="left" class="form-group">
                            <label name="confirm_password" class="cols-sm-2 control-label">Confirm Password</label>
                            <div class="cols-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
                                    <input type="password" class="form-control" name="confirm_password"   placeholder="Confirm your Password"/>
                                </div>
                            </div>
                        </div>enter code here

                        <div align="left" class="form-group ">
                            <button  type="button" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>
                        </div>
                        <div class="login-register">
                            <a href="log.php">Already registered? Login here</a>
                         </div>
                    </form>
                </div>
            </div>
        </div>
        </tr>
    </td>
</table>






<?php

    if (isset($_POST['submit']))
{
    $name = $_POST['name'];
    $username = $_POST['user_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['confirm_password'];
    $slquery = "SELECT * FROM customer WHERE email = '$email'";
    $selectresult = mysql_query($slquery);
    if(empty($_POST['name']))
                    {
                        $nameErr='Enter Your Name!';
                    }
                    else 
                    {
                        $user = test_input($_POST['name']);
                        if(!preg_match('/^[a-zA-Z0-9@_]*$/',$user))
                            {
                                $nameErr=' Re-Enter Your Name! Format Inccorrect!( only alpha, numbers,@_ are allowed)';
                            }
                    }
                    if(empty($_POST['password']))
                    {
                        $passErr='Enter Your Password!';
                    }
                    else
                    {
                        $user = test_input($_POST['password']);
                        if(!preg_match('/^[a-zA-Z0-9@_]*$/',$pass))
                        {
                            $passErr='Invalid Format! Re-Enter Password!';
                        }
                    }
    if(mysql_num_rows($selectresult)>0)
    {
         $msg = 'email already exists';
    }
    elseif($password != $cpassword){
         $msg = "passwords doesn't match";
    }
    else{
          $query = "INSERT INTO customer (name,email,user_name, password,confirm_password, ) VALUES ('
          $name', '$username', '$password', '$cpassword', '$email')";
          $result = mysql_query($query);
          if($result){
             $msg = "User Created Successfully.";
          }
    }
   }

?>


</body>
</html>

我正在尝试使用validation.i创建一个注册表单,前端设计使用一些引导程序,然后使用PHP的Mysql数据库。

当我点击提交注册表单时,它不会验证字段值。请某人告诉我这是错误的。还有,它没有显示错误。我不知道为什么这不起作用。

Thank you in Advance.

2 个答案:

答案 0 :(得分:2)

只需从

更改此行
<button  type="button" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>

<button  type="submit" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>

您已使用button,因此未提交表单。将其更改为submit类型

修改

注意:请开始使用PDO或mysqli

请检查以下点:

1)您使用了test_input()但未在显示的代码中定义。

2)您的查询在,后会有额外的confirm_password

3)插入查询不起作用。有一些错误:

$query = "INSERT INTO customer (name,user_name, password,confirm_password, email) VALUES ('
          $name', '$username', '$password', '$cpassword', '$email')";

4)无需存储confirm_password

答案 1 :(得分:0)

<强>数据库

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "stackoverflow";
// Create connection
$conn = new mysqli($servername, $username, $password,$database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
    session_start();
?>

像这样更改“注册”按钮

<div align="left" class="form-group ">
                        <button  type="submit" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>
                    </div>

<强> test_function

  function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
  }

完整的PHP

<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$username = $_POST['user_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirm_password'];
$slquery = "SELECT * FROM customer WHERE email = '$email'";
$selectresult = mysqli_query($conn,$slquery);
 if(empty($_POST['name']))
                {
                    $nameErr='Enter Your Name!';
                }
                else 
                {
                    $user = test_input($_POST['name']);
                  if(!preg_match('/^[a-zA-Z0-9@_]*$/',$user))
                        {
                            $nameErr=' Re-Enter Your Name! Format 
    Incorrect!( only alpha, numbers,@_ are allowed)';
                        }
                }
                if(empty($_POST['password']))
                {
                    $passErr='Enter Your Password!';
                }
                else
                {
                    $user = test_input($_POST['password']);
                    if(!preg_match('/^[a-zA-Z0-9@_]*$/',$password))
                    {
                        $passErr='Invalid Format! Re-Enter Password!';
                    }
                }

 if (mysqli_num_rows($selectresult)>0)
    {
     echo 'email already exists';
    }               

elseif($password != $cpassword){
     $msg = "passwords doesn't match";
}
else{

      var_dump($name.$email.$username.$password.$cpassword);
      $query = "INSERT INTO customer(name,email,user_name, 
      password,confirm_password) VALUES ('
      $name', '$email','$username', '$password', '$cpassword')";
        if ($conn->query($query) === TRUE) {
            echo "New user created successfully";
        } else {
            echo "Error: " . $query . "<br>" . $conn->error;
        }

        $conn->close();
   }
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}?>