2017-08-29 56 views
0
<!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('/^[[email protected]_]*$/',$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('/^[[email protected]_]*$/',$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使用的一些引導前端的設計,則Mysql數據庫和PHP沿isset功能無法正常工作。我不知道爲什麼,當我提交

當我點擊提交註冊表單時,它不會驗證字段values.please,有人告訴我這有什麼問題,並且它沒有顯示錯誤。我不知道爲什麼這不起作用。

Thank you in Advance. 
+0

你爲什麼要使用表單操作'htmlspecialchars'? –

+0

使用'error_reporting(E_ALL)'並向我們顯示錯誤 – droidnation

+0

Plus,使用'MySQLi'或'PDO'代替這些舊的棄用函數 – droidnation

回答

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

+0

謝謝Jigar shah,當它改變它的類型時,它工作...但它顯示了一些錯誤,我沒有使用任何功能 –

+1

你能編輯你的帖子並粘貼錯誤,所以我們可以幫你。還要確保有錯誤報告 –

+0

致命錯誤:調用未定義的函數test_input()在第129行......這是它顯示的錯誤... –

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('/^[[email protected]_]*$/',$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('/^[[email protected]_]*$/',$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; 
}?> 
+0

我已經按照您的建議代碼做了所有事情......但它不驗證輸入值..... –

+0

我只更改了數據庫連接代碼,test_input函數,提交按鈕和INSERT INTO數據庫部分,其餘是你的代碼。我認爲你有責任去做其餘的工作。你不這麼認爲嗎? – ovicko

+0

當然是的,我的工作......我只是想知道我是否正確的方式..... –

相關問題