Jump to content

basic php help ... please!!


khalen

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/98722-basic-php-help-please/
Share on other sites

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,

Link to comment
https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505190
Share on other sites

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 ;)

Link to comment
https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505191
Share on other sites

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!!

Link to comment
https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505198
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505201
Share on other sites

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";

 

?>

Link to comment
https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505205
Share on other sites

$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.

 

;D

Link to comment
https://forums.phpfreaks.com/topic/98722-basic-php-help-please/#findComment-505210
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.