2016-06-04 4 views
-3

Ich möchte überprüfen, ob ein Benutzer über meine Adressleiste mit der GET-Methode existiert. Wenn diese Benutzer existieren, dann echo $ username und $ firstname. aber es zeigt 2 Fehler an. Ich weiss nicht, was falsch ist!.Undefinierte Variable: Benutzername in C: xampp htdocs test.php in Zeile 26

<?php include ("./trial/header.php"); ?> 
<?php 
    //we want to perform http://localhost/test.php?=jude to see if user exists then displays username and first name 
    //the table name is users and include username, first_name, last_name, password and email. 
if (isset($_GET['u'])) { 
    $username = mysqli_real_escape_string($con, $_GET['u']); 
    if (ctype_alnum($username)) { 
    //check if user exists 
    $check = mysqli_query($con, "SELECT username, first_name FROM users WHERE username='$username'"); 
    //if user exists we want to display username and firstname on the page 
    if (mysqli_num_rows($check)===1) { 
    $get = mysqli_fetch_assoc($check); 
    $username = $get['username']; 
    $firstname = $get['firstname']; 
    } 
    //if user do not exist we want to to display "The user does not exist" 
    else 
    { 
    echo "The user does not exist"; //no existing users 
    exit(); 
    } 
    } 
} 
?> 

    <h2>Profile page for: <?php echo $username;?></h2>; 
    <h2>First name: <?php echo $firstname;?></h2>; 
+0

Geben Sie auch Ihre Fehlermeldung ein – Saty

+2

'Vorname' ist nicht gleich' Vorname' – Saty

+0

Welchen Fehler haben Sie? –

Antwort

0

Sie haben Probleme in Ihrem if statement Ihr Code nicht in sie eintritt ... einer Vermeidung des Problems ich Ihnen Code geben kann ...

<?php include ("./trial/header.php"); ?> 
<?php 
    //we want to perform http://localhost/test.php?=jude to see if user exists then displays username and first name 
    //the table name is users and include username, first_name, last_name, password and email. 
$username = ""; //Added this 
$firstname = "";//Added this 
if (isset($_GET['u'])) 
{ 
    $username = mysqli_real_escape_string($con, $_GET['u']); 
    if (ctype_alnum($username)) 
    { 
     //check if user exists 
     $check = mysqli_query($con, "SELECT username, first_name FROM users WHERE username='$username'"); 
     //if user exists we want to display username and firstname on the page 
     if (mysqli_num_rows($check)===1) 
     { 
      $get = mysqli_fetch_assoc($check); 
      $username = $get['username']; 
      $firstname = $get['first_name']; 
     } 
     //if user do not exist we want to to display "The user does not exist" 
     else 
     { 
      echo "The user does not exist"; //no existing users 
      exit(); 
     } 
    } 
} 
?> 

<h2>Profile page for: <?php echo $username;?></h2>; 
<h2>First name: <?php echo $firstname;?></h2>; 

Für eine korrekte Ausgabe Ihrer sollte verwenden Diese URL http://localhost/test.php?u=jude anstelle von http://localhost/test.php?=jude Als Md. Sahadat Hossain Vorgeschlagen.

Verwandte Themen