Jump to content

[SOLVED] Register Check, Easy, Just need another pair of eyes!


Recommended Posts

Hello everyone,

 

I have a register submit form and a register check form. The register check form is supposed to send back what went wrong. and the register form is supposed to display the issues to the user.

 

my registercheck.php file looks like this:

<?php
session_start();
$host="localhost";
$username="username";
$password="password"; 
$db_name="wezzsmusic";
$tbl_name="wmusers";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$errors = array();

if (empty($_POST['username']))
{
     $errors[] = 'Please supply a username';
}
if (empty($_POST['password']) || strlen($_POST['password']) < 6)
{
     $errors[] = 'You entered an invalid password';
}
if (empty($_POST['email']) || strpos($_POST['email'], '@') === false)
{
     $errors[] = 'You entered an invalid email';
}

$check = "SELECT * FROM wmusers where username='".$_POST['username']."' LIMIT 1";
$checkresult = mysql_query($check);

if (mysql_num_rows($checkresult) != 0)
{
     $errors[] = 'You entered a taken username';
}

$check2 = "SELECT * FROM wmusers where email='".$_POST['email']."' LIMIT 1";
$checkresult2 = mysql_query($check2);

if (mysql_num_rows($checkresult2) != 0)
{
     $errors[] = 'You entered a taken email';
}

if (count($errors))
{
     header("Location: register.php");
     exit;
}
else
{
     $sql = "INSERT INTO wmusers VALUES ('0', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '0')";
     mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error()); 
}
?>

 

how would i send back the problems to register.php. I've never really worked with arrays.

 

Thanks

 

Snooble

<?php
session_start();
$host="localhost";
$username="username";
$password="password"; 
$db_name="wezzsmusic";
$tbl_name="wmusers";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$errors = array();
$_SESSION['errors']= array(); //ADDED

if (empty($_POST['username']))
{
     $errors[] = 'Please supply a username';
}
if (empty($_POST['password']) || strlen($_POST['password']) < 6)
{
     $errors[] = 'You entered an invalid password';
}
if (empty($_POST['email']) || strpos($_POST['email'], '@') === false)
{
     $errors[] = 'You entered an invalid email';
}

$check = "SELECT * FROM wmusers where username='".$_POST['username']."' LIMIT 1";
$checkresult = mysql_query($check);

if (mysql_num_rows($checkresult) != 0)
{
     $errors[] = 'You entered a taken username';
}

$check2 = "SELECT * FROM wmusers where email='".$_POST['email']."' LIMIT 1";
$checkresult2 = mysql_query($check2);

if (mysql_num_rows($checkresult2) != 0)
{
     $errors[] = 'You entered a taken email';
}

$_SESSION['errors'] = $errors; //ADDED
if (count($errors))
{
     header("Location: register.php");
     exit;
}
else
{
     $sql = "INSERT INTO wmusers VALUES ('0', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '0')";
     mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error()); 
}
?>

 

in register.php

 

<?php
session_start();
echo "<pre>";
print_r($_SESSION['errors']);
?>

 

**UNTESTED

Try this one.

 

<?php

session_start();

$host="localhost";

$username="username";

$password="password";

$db_name="wezzsmusic";

$tbl_name="wmusers";

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$errors = "";

$errors = array();

 

if (empty($_POST['username'])){

    $errors[] = 'Please supply a username';

}

if (empty($_POST['password']) || strlen($_POST['password']) < 6){

    $errors[] = 'You entered an invalid password';

}

if (empty($_POST['email']) || strpos($_POST['email'], '@') === false){

    $errors[] = 'You entered an invalid email';

}

 

$check = "SELECT * FROM wmusers where username='".$_POST['username']."' LIMIT 1";

$checkresult = mysql_query($check);

 

if (mysql_num_rows($checkresult) != 0){

    $errors[] = 'You entered a taken username';

}

 

$check2 = "SELECT * FROM wmusers where email='".$_POST['email']."' LIMIT 1";

$checkresult2 = mysql_query($check2);

 

if (mysql_num_rows($checkresult2) != 0){

    $errors[] = 'You entered a taken email';

}

 

if (!empty($errors)){

    header("Location: register.php?".http_build_query($errors));

    exit;

}

else{

    $sql = "INSERT INTO wmusers VALUES ('0', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '0')";

    mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error());

}

?>

 

register.php

<?

# Print Error Msg

print $_GET['0'];

print $_GET['1'];

print $_GET['2'];

print $_GET['3'];

print $_GET['4'];

?>

 

Let me know if it's work

MadTechie...

 

That looks good to me... but the output to the user looks like this:

 

Array
(
    [0] => Please supply a username
    [1] => You entered an invalid password
    [2] => You entered an invalid email
    [3] => You entered a taken username
    [4] => You entered a taken email
)

 

i dont want the Array nor the () bits.

 

nor the [number] => bits.

 

thanks though

 

Snooble

humm try

<?php
session_start();
$errors =$_SESSION['errors'];
//var_dump($errors); //uncomment if doesn't work and post the output
if(count($errors) > 0)
{
foreach($errors as $errorlog)
{
	echo "$errorlog<br>";
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.