2017-04-15 3 views
-1

Ich schreibe den Code für eine Anmeldung. Ich habe den Code von einer anderen Ressource bekommen. Alles scheint in Ordnung zu sein, aber es sagt mir immer wieder, dass die Alumni_ID (der Benutzername) nicht existiert - obwohl sie es tut.PHP-Login-Code mit Benutzername Fehler

Der PHP-Code für das Login:

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

 
$servername = getenv('IP'); 
 
$username = getenv('C9_USER'); 
 
$password = ""; 
 
$database = "c9"; 
 
$dbport = 3306; 
 
    $conn = mysqli_connect($servername,$username,"",$database,$dbport) or die(mysqli_error()); 
 
    if($_SERVER["REQUEST_METHOD"] == "POST"){ 
 
\t 
 
\t 
 
\t $query = mysql_query("SELECT Alumni_ID, Password from 'Alumni' WHERE Alumni_ID='$Alumni_ID'"); //Query the Alumni table if there are matching rows equal to $Alumni_ID 
 
\t 
 
\t $exists = mysql_num_rows($query); //Checks if Alumni_ID exists 
 
\t $table_Alumni = ""; 
 
\t $table_Password = ""; 
 
\t 
 
\t if($exists = 0) //IF there are no returning rows or no existing Alumni_ID 
 
\t 
 
\t  { 
 
       \t \t while($row = mysql_fetch_assoc($query)) //display all rows from query 
 
       \t \t { 
 
       \t \t \t $table_Alumni_ID = $row['Alumni_ID']; // the first Alumni_ID row is passed on to $table_Alumni, and so on until the query is finished 
 
       \t \t \t $table_Password = $row['Password']; // the first Password row is passed on to $table_Alumni, and so on until the query is finished 
 
       \t \t } 
 
       \t \t if(($Alumni_ID == $table_Alumni_ID) && ($Password == $table_Password)) // checks if there are any matching fields 
 
       \t \t { 
 
       \t \t \t \t if($Password == $table_Password) 
 
       \t \t \t \t { 
 
       \t \t \t \t \t $_SESSION['user'] = $Alumni_ID; //set the Alumni_ID in a session. This serves as a global variable 
 
       \t \t \t \t \t header("location: loggedalumni/index.php"); // redirects the user to the authenticated home page 
 
       \t \t \t \t } 
 
       \t \t \t \t 
 
       \t \t } 
 
       \t \t else 
 
       \t \t { 
 
       \t \t \t Print '<script>alert("Incorrect Password!");</script>'; //Prompts the user 
 
       \t \t \t Print '<script>window.location.assign("index.php");</script>'; // redirects to login.php 
 
       \t \t } 
 
\t } 
 
\t else 
 
\t { 
 
\t \t Print '<script>alert("Incorrect Username!");</script>'; //Prompts the user 
 
\t \t Print '<script>window.location.assign("index.php");</script>'; // redirects to login.php 
 
\t } 
 
} 
 
?>

Und das ist die Form:

<div class="col-lg-12"> 
 
           <div class="nav-link" style= "text-align: center;"><h3><b>Log In</b></h3></div> 
 
           <form id="ajax-login-form" action=" " method="post" role="form" autocomplete="off"> 
 
            <div class="form-group"> 
 
             <label for="Alumni_ID" style= "text-align: center;">Alumni ID</label> 
 
             <input type="text" name="Alumni_ID" tabindex="1" class="form-control" placeholder="Your Alumni ID" value="" autocomplete="off"> 
 
            </div> 
 

 
            <div class="form-group"> 
 
             <label for="password" style= "text-align: center;">Password</label> 
 
             <input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="Password" autocomplete="off"> 
 
            </div> 
 

 
            <div class="form-group"> 
 
             <div class="row row-centered" style="padding-left:20px"> 
 
              <div class="col-xs-7"> 
 
               <input type="checkbox" tabindex="3" name="remember" id="remember"> 
 
               <label for="remember" style= "align: center;"> Remember Me</label> 
 
              </div> 
 
              </div> 
 
              <div class="row row-centered"> 
 
              <div class="col-xs-5" style="padding-left:75px"> 
 
               <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-success" style="background-color: #6a0001;" value="Log In" align="middle"> 
 
              </div> 
 
             </div> 
 
            </div> 
 

 
            <div class="form-group"> 
 
             <div class="row"> 
 
              <div class="col-lg-12"> 
 
               <hr> 
 
               <div style="padding-left:10px"> 
 
                <a href="recover.php" tabindex="5" class="forgot-password" style = "color: black">Forgot Password?</a> 
 
               </div> 
 
               <hr> 
 
               <div style="padding-left:10px"> 
 
                <a href="NewAlumni.php" tabindex="5" class="forgot-password" style = "color: black">Register Account</a> 
 
               </div> 
 
               <hr> 
 
               <div style="padding-left:10px"> 
 
                <a href="adminlogin.php" tabindex="5" class="forgot-password" style = "color: black">Admin Login</a> 
 
               </div> 
 

 
              </div> 
 
             </div> 
 
            </div> 
 
            <input type="hidden" class="hide" name="token" id="token" value="a465a2791ae0bae853cf4bf485dbe1b6"> 
 
           </form> 
 
          </div>

+0

Ist das ein PHP-Fehler oder SQL-Fehler? Können Sie die eigentliche Fehlermeldung einfügen? Versuchen Sie, 'print' SELECT Alumni_ID, Passwort aus 'Alumni' WHERE Alumni_ID = '$ Alumni_ID' "; in Code und führen Sie dann die Ausgabe-Anweisung direkt in SQL. – Manngo

+0

Sie verwenden MySQL-Funktionen. Sie sind abgeschrieben und unterliegen Hacking. Verwenden Sie stattdessen mysqli-Funktionen. –

+0

@SloanThrasher ist richtig, aber ich sehe, dass sie mit mysqli_-Anweisungen gemischt werden, was noch schlimmer ist. Siehe http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php, um herauszufinden, wie dies zu einem Fehler führen kann. – Manngo

Antwort

-1

Sie definieren nicht, was die va $Aliumni_ID riablen ist

$query = mysql_query("SELECT Alumni_ID, Password from 'Alumni' WHERE Alumni_ID='$Alumni_ID'"); 

Eine unsachgemäße aber funktionierenden Code ist

$query = mysql_query("SELECT Alumni_ID, Password from 'Alumni' WHERE Alumni_ID='".$_POST['Alumni_ID']."'"); 
+0

Ein "geeigneterer Code" wäre die Verwendung vorbereiteter Anweisungen mit Abfrageparametern. Sie befürworten injizierbaren SQL-Code. – David

+0

Wahr, es erlaubt ein großes Problem mit der Injektion – Forbs

Verwandte Themen