I'm trying to display the "name" of a user who has successfully logged in to my website. It is based a simple open source script.
Here's the fully working unmodified code which displays the name, uc as needed:
<?php session_start(); $dbhost = "localhost"; $dbname = "member1"; $dbuser = "root"; $dbpass = ""; mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error()); mysql_select_db($dbname) or die("MySQL Error: " . mysql_error()); ?> <?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['uc'])) { ?> <h3>Welcome</h3> <p>your uc is <strong><?=$_SESSION['uc']?></strong></p> <p>your name is <strong><?=$_SESSION['name']?></strong></p> <a href="logout.php">logout</a> <?php } elseif(!empty($_POST['uc']) && !empty($_POST['mobile'])) { $uc = mysql_real_escape_string($_POST['uc']); $name = mysql_real_escape_string($_POST['name']); $mobile = md5(mysql_real_escape_string($_POST['mobile'])); $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND name = '".$name."' AND mobile = '".$mobile."'"); if(mysql_num_rows($checklogin) == 1) { $row = mysql_fetch_array($checklogin); $_SESSION['uc'] = $uc; $_SESSION['name'] = $name; $_SESSION['LoggedIn'] = 1; echo "<h1>Success - redirecting</h1>"; echo "<meta http-equiv='refresh' content='2;rdvmembers-index.php' />"; } else { echo "<h1>Error</h1>"; } } else { ?> <h1>Members Login</h1> <form method="post" action="rdvmembers-index.php" name="loginform" id="loginform"> uc <input type="text" name="uc" id="uc" size="40" /> name <input type="text" name="name" id="name" size="40" /> mobile <input type="text" name="mobile" id="mobile" size="40"/> <input class="button_text" type="submit" name="login" id="login" value="Login" /> </form> <?php } ?> </div> </body> </html>
Now, i want to remove the name field from the "Members Login" form so that it ask for only UC and Mobile. The name to be displayed must be taken from the database on successful login. I modified the form and made a few changes to the code but no matter what changes i do, it won't display the "name". Where am i going wrong?
here's the modified code:
<?php session_start(); $dbhost = "localhost"; $dbname = "member1"; $dbuser = "root"; $dbpass = ""; mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error()); mysql_select_db($dbname) or die("MySQL Error: " . mysql_error()); ?> <?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['uc'])) { ?> <h3>Welcome</h3> <p>your uc is <strong><?=$_SESSION['uc']?></strong></p> <p>your name is <strong><?=$_SESSION['name']?></strong></p> <a href="logout.php">logout</a> <?php } elseif(!empty($_POST['uc']) && !empty($_POST['mobile'])) { $uc = mysql_real_escape_string($_POST['uc']); $name = mysql_real_escape_string($_POST['name']); $mobile = md5(mysql_real_escape_string($_POST['mobile'])); $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND name = '".$name."' AND mobile = '".$mobile."'"); if(mysql_num_rows($checklogin) == 1) { $row = mysql_fetch_array($checklogin); $_SESSION['uc'] = $uc; $_SESSION['name'] = $name; $_SESSION['LoggedIn'] = 1; echo "<h1>Success - redirecting</h1>"; echo "<meta http-equiv='refresh' content='2;rdvmembers-index.php' />"; } else { echo "<h1>Error</h1>"; } } else { ?> <h1>Members Login</h1> <form method="post" action="rdvmembers-index.php" name="loginform" id="loginform"> uc <input type="text" name="uc" id="uc" size="40" /> mobile <input type="text" name="mobile" id="mobile" size="40"/> <input class="button_text" type="submit" name="login" id="login" value="Login" /> </form> <?php } ?> </div> </body> </html>
ERROR:
Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 28
If i remove line 28, the error goes to line 30 and so forth. Can anyone figure this out for me? It seems plain simple.