khalen Posted March 30, 2008 Share Posted March 30, 2008 Hi guys ... I am just starting to learn php and I am trying to write a bit of code to take a name from a user and compare it to names in a database, (array at the moment) and return a message depending on the name being there or not. Ultimately I want to write code to allow a user to either sign in or register to use some facilities of a website. The code I have done doesn't return name when it is included in array only not a valid name message. Please can anyone tell me what I am doing wrong??? <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> <input type="submit" /> </form> </body> </html> <html> <body> <?php $names = array("Paul","Maria","Khalen","Conall","Anaria"); if ($names==$_post["name"]) echo "Welcome ",$name; else echo "Sorry, that is not a valid name"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/ Share on other sites More sharing options...
uniflare Posted March 30, 2008 Share Posted March 30, 2008 does not work because $_post['name'] is not an array which means it is not equal to $names. add an array_flip to flip the values and keys of the array. eg: <html> <body> <?php $names = array("Paul","Maria","Khalen","Conall","Anaria"); $name = array_flip($names); if ($names[$_post["name"]] != null) echo "Welcome ",$name; else echo "Sorry, that is not a valid name"; ?> </body> </html> hope this helps, Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505190 Share on other sites More sharing options...
ds111 Posted March 30, 2008 Share Posted March 30, 2008 i would do it like this: <?php function checkforusers(); { $user $_POST['username']; $mysql = "SELECT * FROM yourtablename WHERE username='$user'"; $query = mysql_query($mysql); $num = mysql_numrows($query); if($num == 0) { echo "There were no users found with that username"; } else { echo "There were users found with that username...$num of them."; } } try that Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505191 Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 or you could use in_array() function <?php $names = array("Paul","Maria","Khalen","Conall","Anaria"); if (in_array($_POST["name"], $names)) echo "Welcome ",$name; else echo "Sorry, that is not a valid name"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505196 Share on other sites More sharing options...
khalen Posted March 30, 2008 Author Share Posted March 30, 2008 does not work because $_post['name'] is not an array which means it is not equal to $names. add an array_flip to flip the values and keys of the array. Thanks very much for your help ... I copied your code into my piece and tried it. Unfortunately I get the Sorry not a valid name regardless of what name I put in. Even a legitimate name returns same message!! Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505198 Share on other sites More sharing options...
ds111 Posted March 30, 2008 Share Posted March 30, 2008 try mine. also, can someone please read this? http://www.phpfreaks.com/forums/index.php/topic,190033.0.html Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505200 Share on other sites More sharing options...
khalen Posted March 30, 2008 Author Share Posted March 30, 2008 or you could use in_array() function <?php $names = array("Paul","Maria","Khalen","Conall","Anaria"); if (in_array($_POST["name"], $names)) echo "Welcome ",$name; else echo "Sorry, that is not a valid name"; ?> Closest yet ... give me everything I want except the name after the welcome! thanks for trying though! Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505201 Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 $name isn't defined <?php $names = array("Paul","Maria","Khalen","Conall","Anaria"); if (in_array($_POST["name"], $names)) echo "Welcome ",$_POST['name']; else echo "Sorry, that is not a valid name"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505204 Share on other sites More sharing options...
jack5100nv Posted March 30, 2008 Share Posted March 30, 2008 This is what you need to do <?php $names = array("Paul","Maria","Khalen","Conall","Anaria"); $i=0; while($names[$i]==true) { if ($names[$i]==$_post["name"]) { $logged = $names[$i]; break; } $i++; } if($logged == true) { echo("Welcome $logged"); } else echo "Sorry, that is not a valid name"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505205 Share on other sites More sharing options...
Bladescope Posted March 30, 2008 Share Posted March 30, 2008 <?php $name = $_POST['name']; $names = array("Paul","Maria","Khalen","Conall","Anaria"); foreach($names as $value) { if ($value == $name) { $logged = TRUE; } if ($logged) { echo "Welcome, $name"; } else { echo "Sorry, username $name does not exist."; } ?> Clean. Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505209 Share on other sites More sharing options...
khalen Posted March 30, 2008 Author Share Posted March 30, 2008 $name isn't defined <?php $names = array("Paul","Maria","Khalen","Conall","Anaria"); if (in_array($_POST["name"], $names)) echo "Welcome ",$_POST['name']; else echo "Sorry, that is not a valid name"; ?> Got it thanks a million. I have NEVER had so much responce to a single post from anywhere before. Thanks a million guys. Hope I can call on you again sometime. Quote Link to comment https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505210 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.