Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem in form after migrate to php 5

Status
Not open for further replies.

soulow

Programmer
Jan 23, 2011
4
BG
Hello, I am new in php..here is my problem. I updated my server to the latest php and now I have problem with my code, it was working fine before, but after update i got errors like that
Code:
 Notice: Undefined index:
and it seems that the function VerifyForm doesn't returns the values.
here is my code:
Code:
<?php
include("config.php");


function VerifyForm($values, $errors)
{
    // checks
	$nick_check = $values['nick'];
    $nick_check2 = mysql_query("Select user_id from users where user_name='$nick_check'");
	while($nick_check = mysql_fetch_array($nick_check2))
		{
			if($nick_check['user_id']>0)
				$errors['nick'] = 'The username is alreade in use';
		}
    if (strlen($values['nick']) < 3)
        $errors['nick'] = 'The username is too short';
    elseif (strlen($values['nick']) > 32)
        $errors['nick'] = 'The username is too long';


    if (!explode('.*@.*\..{2,4}', $values['mail']))
        $errors['mail'] = 'Email address invalid';

	$mail_check = $values['mail'];
    $mail_check2 = mysql_query("Select user_id from users where user_mail='$mail_check'");
	while($mail_check = mysql_fetch_array($mail_check2))
		{
			if($mail_check['user_id']>0)
				$errors['mail'] = 'The Email is already in use';
		}


    if (strlen($values['password']) < 5)
        $errors['password'] = 'The password must be at least 5 symbols';
    
	elseif ($values['password'] != $values['password2'])
        $errors['password2'] = 'The passwords didn\'t match';


	if ( $values['c'] != $values['captcha'])
        $errors['captcha'] = 'Wrong answer!';
    return (count($errors) == 0);
}

function DisplayForm($values, $errors)
{
	srand (time(NULL));
	$a = rand(1, 10);
	$b = rand(1, 10);
	$c=$a+$b;

    if(!isset($errors))
	{ $errors=null;}
	
	?>
    <html>
    <head>
        <title>Register New User</title>
        <style>
            TD.error
            {
                color: red;
                font-weight: bold;    
            }
        </style>
    </head>
	<body>
 <?php
    if (count($errors) > 0) 
        echo "<p><font color='red'>There were some errors, please check again</font></p>";
 ?>    

<form action="<?php  echo $_SERVER['PHP_SELF'];  ?>"  method="post">
 <table>
        <tr>
            <td>Username:</td>
			<td><input type="text" name="nick" value="<?php htmlentities($values['nick']) ?>"/>
			<td class="error"><?php $errors['nick'] ?></td>
        </tr>
		<tr>
            <td>Email:</td>
			<td><input type="text" name="mail" value="<?php htmlentities($values['mail']) ?>"/>
			<td class="error"><?php $errors['mail'] ?></td>
        </tr>
		<tr>
            <td>Password:</td>
			<td><input type="password" name="password" value="<?php htmlentities($values['password']) ?>"/>
			<td class="error"><?php $errors['password'] ?></td>
        </tr>
		<tr>
            <td>Confirm:</td>
			<td><input type="password" name="password2" value="<?php htmlentities($values['password2']) ?>"/>
			<td class="error"><?php $errors['password2'] ?></td>
        </tr>
		<tr>	
			<td><?php echo $a.'+'.$b; ?> = </td>
			<input type="hidden" name="c" value="<?php echo $c ?>"/>
			<td><input type="text" name="captcha" value="<?php htmlentities($values['captcha']) ?>"/>
			<td class="error"><?php $errors['captcha'] ?></td>
		</tr>
		<tr>
			<td colspan="2" align="center"><input type="submit" value="Submit">
		</tr>
    </table>
    </form>

    </body>
    </html>
    <?php 
}


//make the insert
function ProcessForm($values)
{
$nick = $values['nick'];
$password = $values['password'];
$mail = $values['mail'];
$id = mysql_query("Select max(user_id) from users") ;
$user_id = mysql_fetch_array($id);
$user_id = $user_id['max(user_id)']+1;
$ip = GetHostByName($REMOTE_ADDR);
$password = sha1(md5(substr($nick, 0, 3) . $password));
    mysql_query("Insert into users values
	(
	$user_id,
	'$nick',
	'$password',
	'$mail',
	now(),
	INET_ATON('$ip'),
	1,
	1
	)
	");
	echo "<a href='index.php'>index</a>";
}



if ($_SERVER['REQUEST_METHOD'] == 'POST')
{	
	
    $formValues = $_POST;
    $formErrors = array();
	
	$formValues['nick'] = stripslashes($formValues['nick']);
	$formValues['password'] = stripslashes($formValues['password']);
	$formValues['nick'] = mysql_real_escape_string($formValues['nick']);
	$formValues['password'] = mysql_real_escape_string($formValues['password']);


    if (!VerifyForm($formValues, $formErrors))
	{
		DisplayForm($formValues, $formErrors );
	}
	else
        ProcessForm($formValues);
}
else
{
    DisplayForm(null, null);
}
?>
the file config.php is the information for the mysql db

thank you in advice
 
Seems to me, the update simply allowed the Notices to show by changing the display_errors directive in php.ini, but they were always there. Your code is really badly designed, and attempts to use an array it never actually sets in the scope its trying to use it; mainly the $errors array variable. Additionally, your code would never have worked the way you expected it work as all your attempts to actually output any contents from the $errors array are wrong.

You set all these values in $errors variable inside the function VerifyForm(), but then forget all about it. When you attempt to use it inside your function DisplayForm it is of course not there. The only thing that is there is the empty array $formErrors you passed to that function when you called it.

Variables in PHP have what's called "scope" that is they exist in certain areas of your script. Variables inside functions exist only inside the functions unless you expressly return them to the outside of the function.

Example:
Code:
fucntion myFunction(){
$myvar['index']=1;
}

$echo myvar['index']; [green]\\This would produce a Notice undefined Index because the variable being used only exists inside the preceding function[/green]

The myvar variable exists inside the function myFunction().

If I tried to use it outside the function (as shown above) or in another function it would not be there.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top