2017-06-04 79 views
-1

我正在製作一個表格,希望在勾選或選中該複選框時可以提交表格。當我登錄不從這個只有當您打勾複選框時纔會使用表格

注意除了檢查箱子我沒有得到任何其他錯誤:未定義指數:stayLoggedIn在/ home /網站/ 2A/d/dbcf5d6440 /的public_html/MySQL的/挑戰/ Secretdiary .php 91行

這是我的代碼!

<?php 

session_start(); 

$error=""; 

if (array_key_exists("logout", $_GET)) { 

    unset($_SESSION); 
    setcookie("id", "", time() - 60*60); 
    $_COOKIE["id"] = ""; 

} else if(array_key_exists("id", $_SESSION) OR array_key_exists("id", $_COOKIE)) { 

    header("Location: loggedinpage.php"); 
} 

    if(array_key_exists("submit", $_POST)) { 

     $link = mysqli_connect("XXX", "XXX", "XXX", "XXX"); 

      if (mysqli_connect_error()) { 

       die("Database Connection Error"); 
      } 



     if(!$_POST['email']) { 

      $error.= "An Email Address is required<br>"; 
     } 

     if(!$_POST['password']) { 

      $error.= "A Password is required<br>"; 
     } 

     if($error != "") { 

      $error = "<p>There were error(s) in your form</p>".$error; 

     } else { 

      if($_POST['signUp'] == '1') { 

      $query = "SELECT id FROM `users` WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."' LIMIT 1"; 
      $result = mysqli_query($link, $query); 
      if (mysqli_num_rows($result) > 0) { 

       $error = "That email address is taken."; 

      } else { 

       $query = "INSERT INTO `users` (`email`, `password`) VALUES('".mysqli_real_escape_string($link, $_POST['email'])."', '".mysqli_real_escape_string($link, $_POST['password'])."')"; 

       if (!mysqli_query($link, $query)) { 

        $error="<p>Could not sign you up, please try again</p>"; 

       } else { 

        $query = "UPDATE `users` SET password = '".md5(md5(mysqli_insert_id($link)).$_POST['password']). "' WHERE id = ".mysqli_insert_id($link)." LIMIT 1"; 

        mysqli_query($link, $query); 
        $_SESSION['id'] = mysqli_insert_id($link); 
        if ($_POST['stayLoggedIn'] == '1') { 

         setcookie("id", mysqli_insert_id($link), time() + 60*60*24*365); 
        } 


       } header("Location: loggedinpage.php"); 


      } 


     } else { 

       $query = "SELECT * FROM `users` WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."'"; 

       $result = mysqli_query($link, $query); 
       $row = mysqli_fetch_array($result); 
       if (array_key_exists("id", $row)) { 

        $hashedPassword = md5(md5($row['id']).$_POST['password']); 
        if ($hashedPassword == $row['password']) { 

         $_SESSION['id'] = $row['id']; 
         if ($_POST['stayLoggedIn'] == '1') { 

          setcookie("id", $row ($link), time() + 60*60*24*365); 
        } 
        } 
       } 

       } 


    } 

    } 



?> 




<div id="error"><?php echo $error; ?></div> 
<form method="post"> 
    <input type="email" name="email" placeholder="Your Email Eg. [email protected]"> 
    <input type="password" name="password" placeholder="Password"> 
    <input type="checkbox" name="stayLoggedIn" value=1> 
    <input type="hidden" name="signUp" value="1"> 
    <input type="submit" name="submit" value="Sign Up!"> 
</form> 

<form method="post"> 
    <input type="email" name="email" placeholder="Your Email Eg. [email protected]"> 
    <input type="password" name="password" placeholder="Password"> 
    <input type="checkbox" name="stayLoggedIn" value=1> 
    <input type="hidden" name="signUp" value="0"> 
    <input type="submit" name="submit" value="Log In!"> 
</form> 
+0

當您提交表單沒有檢查它的價值就不會被提交,這樣你就沒有任何$ _ POST [「stayLoggedIn」]和複選框修復它檢查它是否存在使用isset($ _ POST ['stayLoggedIn'))返回true或false –

+0

我希望你意識到你有更大的問題,而不僅僅是一個複選框的問題,但沒有人抓住這一點。你比這更需要複雜化。 –

+0

MD5不足以進行密碼散列。使用['password_hash()'](http://us3.php.net/manual/en/function.password-hash.php)和['password_verify()'](http://us3.php.net/ manual/en/function.password-verify.php)。 –

回答

1

複選框只有選中時發送的值。因此,您必須執行isset($_POST['stayLoggedIn'])以查看是否選擇了該值,否則該索引將不存在於PHP POST值中。

2

複選框只有在checkecd時纔會作爲參數提交。因此,您需要使用isset()來測試複選框是否已設置,而不是檢查值。因此,改變

if ($_POST['stayLoggedIn'] == '1') 

if (isset($_POST['stayLoggedIn']) 
1

如果未選中複選框,則不發送複選框的值。您可以檢查stayLoggedIn指數isset功能存在與否:

if (isset($_POST['stayLoggedIn']) && $_POST['stayLoggedIn'] == '1') { 
相關問題