pritithangjam Posted September 17, 2013 Share Posted September 17, 2013 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. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 17, 2013 Share Posted September 17, 2013 Recommended: Declare your variables. Or use isset() to check if they are declared before referencing them. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 17, 2013 Share Posted September 17, 2013 @darkfreaks, that's not the answer. did you even read the problem? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 17, 2013 Share Posted September 17, 2013 $_SESSION['uc'] = $row['uc']; $_SESSION['name'] = $row['name']; Quote Link to comment Share on other sites More sharing options...
pritithangjam Posted September 17, 2013 Author Share Posted September 17, 2013 $_SESSION['uc'] = $row['uc']; $_SESSION['name'] = $row['name']; i added your code, same error. i replaced the two lines 37 & 38 with your code, still the same error. Could you be more specific please? Thanks. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 17, 2013 Share Posted September 17, 2013 (edited) what does print_r($row); show? to get undefined index errors from that code, your actual table columns that are being selected must have some letter-case difference from what you are referencing. edit: my reply here of course refers to your CURRENT code that you have posted in some other php help forum that i also visit. Edited September 17, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 17, 2013 Share Posted September 17, 2013 (edited) I'm looking at your second block of code. You are not passing name from the form, so change to this: $uc = mysql_real_escape_string($_POST['uc']); //remove //$name = mysql_real_escape_string($_POST['name']); $mobile = md5(mysql_real_escape_string($_POST['mobile'])); //change this and remove $name $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND mobile = '".$mobile."'"); Edited September 17, 2013 by AbraCadaver Quote Link to comment Share on other sites More sharing options...
pritithangjam Posted September 17, 2013 Author Share Posted September 17, 2013 I'm looking at your second block of code. You are not passing name from the form, so change to this: $uc = mysql_real_escape_string($_POST['uc']); //remove //$name = mysql_real_escape_string($_POST['name']); $mobile = md5(mysql_real_escape_string($_POST['mobile'])); //change this and remove $name $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND mobile = '".$mobile."'"); I did as you asked and got the following error and output: Error: Notice: Undefined variable: name in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 38 Output: Welcome your uc is 999 your name is logout here's the modified code as per your suggestion: <?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']); $mobile = md5(mysql_real_escape_string($_POST['mobile'])); $mobile = md5(mysql_real_escape_string($_POST['mobile'])); $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' 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='5;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> Quote Link to comment Share on other sites More sharing options...
pritithangjam Posted September 17, 2013 Author Share Posted September 17, 2013 what does print_r($row); show? to get undefined index errors from that code, your actual table columns that are being selected must have some letter-case difference from what you are referencing. edit: my reply here of course refers to your CURRENT code that you have posted in some other php help forum that i also visit. yes, that forum was just doing nothing but wasting my time and theirs by coming back to square one time and again. Felt like they are not reading my post but just replying for the sake of it. I don't understand. Where do i place print_r($row);? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 17, 2013 Share Posted September 17, 2013 (edited) $_SESSION['uc'] = $row['uc']; $_SESSION['name'] = $row['name']; You need to do both things that I posted. Edited September 17, 2013 by AbraCadaver Quote Link to comment Share on other sites More sharing options...
pritithangjam Posted September 17, 2013 Author Share Posted September 17, 2013 i get this error when i do that: Notice: Undefined index: uc in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 37 Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 38 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']); $mobile = md5(mysql_real_escape_string($_POST['mobile'])); $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND mobile = '".$mobile."'"); if(mysql_num_rows($checklogin) == 1) { $row = mysql_fetch_array($checklogin); $_SESSION['uc'] = $row['uc']; $_SESSION['name'] = $row['name']; $_SESSION['LoggedIn'] = 1; echo "<h1>Success - redirecting</h1>"; echo "<meta http-equiv='refresh' content='5;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> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 17, 2013 Share Posted September 17, 2013 you would put the print_r($row); statement right after you are fetching the row from the database result set into the $row variable - $row = mysql_fetch_array($checklogin); Quote Link to comment Share on other sites More sharing options...
Solution pritithangjam Posted September 17, 2013 Author Solution Share Posted September 17, 2013 interesting turn of events. I got it fixed! Got help from another forum. It was to do with the database being case sensitive. I changed it to "Name" from "name" and it magically appeared thank you guys for all the help Quote Link to comment Share on other sites More sharing options...
pritithangjam Posted September 17, 2013 Author Share Posted September 17, 2013 wanted to like your posts or thank you for your effort but i'm new here and i have no quota to do so Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 17, 2013 Share Posted September 17, 2013 interesting turn of events. I got it fixed! Got help from another forum. It was to do with the database being case sensitive. I changed it to "Name" from "name" and it magically appeared thank you guys for all the help Yes, the print_r() from 2 hrs ago would've shown this Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.