Php/mysql troubles

okay, i just did this registration code and it says that im calling an undefined function, “valid_email” though i made the function in the beginning, heres the code:


<?php

function filled_out($form_vars)
{
foreach($form_vars as $key => $value)
{
if(!isset($key) || ($value == '')){
return flase;
}
return true;
}

function valid_email($email)
{
if(ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-+\.]+$', $address))
return true;
else
return flase;
}

function db_connect()
{
$result = mysql_pconnect('localhost', 'tucker_tucker', 'raymond');
if(!result)
return false;
if(!mysql_select_db('tucker_users'))

return false;

return $result;

}

function register($username, $email, $password)
{
$conn = db_connect();
if (!$conn)
return 'Could not connect to the databse, please try again later';

$result = mysql_query("select * from user where username='$username'");
}

if(!$result)
return'Could not execute query';
if(mysql_num_rows($result)>0)
return 'That username is taken- go back and chose a different username.';

$result = mysql_query("insert into user values('$username', password('$password'), '$email')");

if (!$result)
return 'Could not register you in database - please try again later.';
return true;
}

$username = $HTTP_POST_VARS['username'];
$email = $HTTP_POST_VARS['email'];
$password = $HTTP_POST_VARS['password'];
$confirm = $HTTP_POST_VARS['confirm'];
session_start();


if(!filled_out($HTTP_POST_VARS)){
echo 'You have not filled out all of the form, please go back and try again';
exit;
}

if(!valid_email($email)){
echo 'The email adress you entered was not valid, please go back and try again';
exit;
}

if(strlen($password)<6 || strlen($password)>16){
echo 'Your password is an invalid length, please go back and try again';
exit;
}

if($password != $confirm){
echo 'Your passwords do not match, please go back and try again';
exit;
}
$reg_result = register($username, $email, $password);
if($reg_result == true){
$HTTP_SESSION_VARS['valid_user'] = $username;

echo 'Your registration was successful!';
}
else{
echo $reg_result;
exit;
}



?>

wait, i fixed it, i forgot to put in brackets WACK

but now it says unexpected $ on line 96, line 96 is:
?>

um i dont see a $ anywhere

I think all your troubles are caused by the left curly bracket at the end of line 7. You don’t have a matching right bracket for it.

okay, i put in all the curly brackets, and it still doesnt work, heres my updated code:


<?php

function filled_out($form_vars)
{
foreach($form_vars as $key => $value)
{
if(!isset($key) || ($value == '')){
return false;
}else{
return true;
}

function valid_email($address)
{
if(ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-+\.]+$', $address)){
return true;
}
else
{
return flase;
}
}

function db_connect()
{
$result = mysql_pconnect('localhost', 'tucker_tucker', 'raymond');
if(!result)
return false;
if(!mysql_select_db('tucker_users')){

return false;
}else{
return $result;
}
}

function register($username, $email, $password)
{
$conn = db_connect();
if (!$conn){
return 'Could not connect to the databse, please try again later';
}
$result = mysql_query("select * from user where username='$username'");

if(!$result){
return'Could not execute query';
}
if(mysql_num_rows($result)>0){
return 'That username is taken- go back and chose a different username.';
}else{
$result = mysql_query("insert into user values('$username', password('$password'), '$email')");

if (!$result){
return 'Could not register you in database - please try again later.';
return true;
}
}

$username = $HTTP_POST_VARS['username'];
$email = $HTTP_POST_VARS['email'];
$password = $HTTP_POST_VARS['password'];
$confirm = $HTTP_POST_VARS['confirm'];
session_start();


if(!filled_out($HTTP_POST_VARS)){
echo 'You have not filled out all of the form, please go back and try again';
exit;
}

if(!valid_email($email)){
echo 'The email adress you entered was not valid, please go back and try again';
exit;
}

if(strlen($password)<6 || strlen($password)>16){
echo 'Your password is an invalid length, please go back and try again';
exit;
}

if($password != $confirm){
echo 'Your passwords do not match, please go back and try again';
exit;
}
$reg_result = register($username, $email, $password);
if($reg_result == true){
$HTTP_SESSION_VARS['valid_user'] = $username;

echo 'Your registration was successful!';
}
else{
echo $reg_result;
exit;
}

?>

okay i went through it and found a bunch of errors, so heres the updated code:


<?php

function filled_out($form_vars)
{
foreach($form_vars as $key => $value)
{
if(!isset($key) || ($value == '')){
return false;
}else{
return true;
}
}

function valid_email($address)
{
if(ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-+\.]+$', $address)){
return true;
}
else
{
return flase;
}
}

function db_connect()
{
$result = mysql_pconnect('localhost', 'tucker_tucker', 'raymond');
if(!result){
return false;
}
if(!mysql_select_db('tucker_users')){
return false;
}else{
return $result;
}
}

function register($username, $email, $password)
{
$conn = db_connect();
if (!$conn){
return 'Could not connect to the databse, please try again later';
}else{
$result = mysql_query("select * from user where username='$username'");
}
if(!$result){
return'Could not execute query';
}
if(mysql_num_rows($result)>0){
return 'That username is taken- go back and chose a different username.';
}else{
$result = mysql_query("insert into user values('$username', password('$password'), '$email')");
}
if (!$result){
echo 'Could not register you in database - please try again later.';
}else{
return true;
}
}

$username = $HTTP_POST_VARS['username'];
$email = $HTTP_POST_VARS['email'];
$password = $HTTP_POST_VARS['password'];
$confirm = $HTTP_POST_VARS['confirm'];
session_start();


if(!filled_out($HTTP_POST_VARS)){
echo 'You have not filled out all of the form, please go back and try again';
exit;
}

if(!valid_email($email)){
echo 'The email adress you entered was not valid, please go back and try again';
exit;
}

if(strlen($password)<6 || strlen($password)>16){
echo 'Your password is an invalid length, please go back and try again';
exit;
}

if($password != $confirm){
echo 'Your passwords do not match, please go back and try again';
exit;
}
$reg_result = register($username, $email, $password);
if($reg_result == true){
$HTTP_SESSION_VARS['valid_user'] = $username;

echo 'Your registration was successful!';
}
else{
echo $reg_result;
exit;
}

?>

Parse error: parse error, unexpected $ in /home2/tucker/public_html/register.php on line 98

In valid_email your have ‘return flase;’.

In db_connect you have ‘if (!result)’. It’s missing a $ sign.

And you are missing a right curly bracket somewhere.

um whats wrong with return false?

Nothing. But you’ve spelt it ‘flase’… :slight_smile:

oh… he he he

ehs, i got through the php troubles, but now im having mysql troubles! ehs this time im stumped:


<?php
//functions

//checks if user filled out all the forms-fns
function filled_out($form_vars){
foreach($form_vars as $key => $value){
if(!isset($key) || ($value == '')){
return false;
}else{
return true;
}
}
}

//checks if the user put in a valid email adress-fns
function valid_email($address)
{
if(ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-z0-9\-\.]+$', $address)){
return true;
}
else
{
return false;
}
}

//connects to a database-fns
function db_connect()
{
$result = mysql_pconnect('localhost', 'tucker_tucker', 'raymond');
if(!$result){
return false;
}
if(!mysql_select_db('tucker_users')){
return false;
}else{
return $result;
}
}

//register the user, checks if username is taken - fns
function register($username, $email, $password)
{
$conn = db_connect();
if (!$conn){
return 'Could not connect to the databse, please try again later';
}else{
$result = mysql_query("select * from users where username='$username'");
}
if(!$result){
return'Could not execute query';
}
if(mysql_num_rows($result)>0){
return 'That username is taken - go back and chose a different username.';
}else{
$result = mysql_query("insert into users values('null', '$username', '$password', '$email')");
}
if (!$result){
echo 'Could not register you in database - please try again later.';
}else{
return true;
}
}

//end of functions


//create short variables
$username = $HTTP_POST_VARS['username'];
$email = $HTTP_POST_VARS['email'];
$password = $HTTP_POST_VARS['password'];
$confirm = $HTTP_POST_VARS['confirm'];
session_start();


if(!filled_out($HTTP_POST_VARS)){
echo 'You have not filled out all of the form, please go back and try again';
exit;
}

if(!valid_email($email)){
echo 'The email adress you entered was not valid, please go back and try again';
exit;
}

if(strlen($password)<6 || strlen($password)>16){
echo 'Your password is an invalid length, please go back and try again';
exit;
}

if(strlen($username)<6 || strlen($username)>16){
echo 'Your username is an invalid length, please go back and try again';
exit;
}

if($password != $confirm){
echo 'Your passwords do not match, please go back and try again';
exit;
}
$reg_result = register($username, $email, $password);
if($reg_result === true){
$HTTP_SESSION_VARS['valid_user'] = $username;

echo 'Your registration was successful!';
}else{
echo $reg_result;
exit;
}
?>

and in my sql file:

use tucker_users

create table users (
customerid int unsigned auto_increment not null primary key,
username varchar(16) not null,
password char(16) not null,
email varchar(100) not null,
);

grant select, insert, update, delete
on tucker_users.*
to tucker_tucker@localhost identified by ‘*******’;

and i get the message, could not execute query, which is from this section of the php file:


$result = mysql_query("select * from users where username='$username'");
}
if(!$result){
return'Could not execute query';
}

but i dont know whats wrong D=

Add the die part. and see whats the result…


$result = mysql_query("select * from users where username='$username'") or die(mysql_error());

Your select looks fine - as long as you don’t put an apostrophe in the username. Then it’ll break.

I’d change the insert to insert into users (username, password, email) values(’$username’, ‘$password’, ‘$email’) to avoid setting customerid to null. Your method might work, but it looks wierd that you try to set customerid to null and you’ve specified that it can’t be null at the same time.

oh he he

Besides that during dev. of youre script its always adviced to add


or die(mysql_error()); 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 45
Could not register you in database - please try again later.Could not register you in the database

heres the part thats having trouble:

//registers the user in the database
function register($username, $email, $password)
{
$connect = db_connect;
if(!$connect){
echo ‘Could not connect to the database, please try again later’;
}
$result = mysql_query(“select * from useres where username=’$username’”);
if(mysql_num_rows($result)>0){
return ‘That username is taken - go back and chose a different username.’;
}else{
$result = mysql_query(“insert into users (username, password, email) values(’$username’, ‘$password’, ‘$email’))”);
}
if (!$result){
echo ‘Could not register you in database - please try again later.’;
}else{
return true;
}
}

inside the function you will have to make the $connect global


global $connect;

when i add that it says i cant connect to the database =X

Well you sure you put in the right db name? are you connecting to a existing db ?

yup

Have you cut and pasted your code? Because then the problem is probably that you’ve misspelled the name of your table. It says ‘useres’…