2017-07-28 45 views
-1
<?php 
function log_page(){ 
    print_r($_POST); 
    //connection variables 
$host = 'localhost'; 
$user = 'root'; 
$password = ''; 
$db = 'firstwebsite'; 

//create mysql connection 
$mysqli = mysqli_connect($host,$user,$password); 
if(!$mysqli){ 
    echo " connection"; 
} 
else{ 
    echo "no connection"; 
} 
$select = mysqli_select_db($mysqli, $db); 

    $_SESSION['message']=''; 

    if (isset($_POST['register'])) { 
    if ($_POST['password'] == $_POST['confirmpassword']) 
    { 
     $$username = $mysqli->real_escape_string($_POST['username']); 
     $email = $mysqli->real_escape_string($_POST['email']); 
     $password = md5($_POST['password']); 

     echo $username . " " . $email . " " . $password; 
    } 
    } 
?> 
<link rel="stylesheet" href="log_style.css" type="text/css"> 
<div class="body-content"> 
    <div class="module"> 
    <h1>Create an account</h1> 
    <form class="form" action="log_page.php" method="post" autocomplete="off"> 
     <input type="text" placeholder="User Name" name="username" required /> 
     <input type="email" placeholder="Email" name="email" required /> 
     <input type="password" placeholder="Password" name="password" autocomplete="new-password" required /> 
     <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required /> 
     <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" /> 
     <input type="submit" value="Already registered? Sign in" name="register2" class="btn btn-block btn-primary" /> 
    </form> 
    </div> 
</div> 
<?php 
} 
?> 

當我要求回顯用戶名和電子郵件和密碼時,我的代碼不會打印任何內容。它沒有輸入$ _POST ['register'] if語句。我認爲PHP代碼在執行表單值之前執行PHP代碼在提交表單之前正在執行

+0

如果這是您的完整代碼,那麼缺少''標記和'confirmpassword' POST數組的輸入。 –

+0

沒有它沒有完整的代碼,他們被定義 – Deema

+0

錯誤是if語句if($ _SERVER [「REQUEST_METHOD」] ==「POST」)沒有輸入 – Deema

回答

0

請勿在表單中使用相同的名稱。如果你沒有上傳表單中的文件,你也不需要enctype="multipart/form-data"

<form class="form" action="log_page.php" method="post" autocomplete="off"> 
       <input type="text" placeholder="User Name" name="username" required /> 
       <input type="email" placeholder="Email" name="email" required /> 
       <input type="password" placeholder="Password" name="password" autocomplete="new-password" required /> 
    <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required /> 
      <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" /> 
      <input type="submit" value="Already registered? Sign in" name="register2" class="btn btn-block btn-primary" /> 
</form> 

請注意,這應該在你的log_page.php;

if ($_POST['register']) { 
    if ($_POST['password'] == $_POST['confirmpassword']) 
    { 
     $username = $mysqli->real_escape_string($_POST['username']); 
     $email = $mysqli->real_escape_string($_POST['email']); 
     $password = md5($_POST['password']); 

echo $username . " " . $email . " " . $password; //test purpose 
    } 
} 
+0

謝謝,但它仍然不工作 注意:未定義的索引:註冊在第8行的C:\ xampp \ htdocs \ test2 \ log_page.php 這是錯誤 – Deema

+0

這不是一個錯誤,只是一個提醒。如果您嘗試在表單中輸入一些值,並輸入相同的密碼1和密碼2,然後點擊註冊按鈕,它就會起作用。 – Slavez

+0

但是它應該如您所建議的那樣回顯名稱,電子郵件和密碼?因爲它不打印任何東西 – Deema

相關問題