Jump to content

[SOLVED] unknown error problem


sudsy1970

Recommended Posts

hi newbie still trying to figure out this php form validation.

 

I have managed to get the form validated and will output the errors on the form if I put some deliberate errors in.

 

however if i don't put any errors in i get the following message:

Notice: Undefined variable: errors in /home/stud/0/0274148/public_html/Assignment/registerdetails.php on line 67

 

but it does connect and find that i have a duplicate entry, but doesn't output the error message as can be seen below.

 

connected to the databaseDuplicate entry 'sudsy' for key 1

 

Why is this please guys.

 

Here is the code

 

<? session_start();

$Firstname = $_POST['Firstname'];
$Surname = $_POST['Surname'];
$Email = $_POST['Email'];
$Dob = $_POST['Dob'];
$Password1 = $_POST['Password1'];
$Password2 = $_POST['Password2'];
$User = $_POST['User'];


if(empty($Firstname))
{
 $errors[]='Please enter your Firstname';
}

if(empty($Surname))
{
 $errors[]='Please enter your Surname';
}

if(empty($User))
{
 $errors[]='You must select a Username';
}

if(empty($Dob))
{
 $errors[]='Date of Birth';
}
	/*elseif(!is_numeric($Dob))
	{
	 $errors[]='Please enter your Date of Birth using numbers...I.E 15/09/2000<br>';
	}*/

if(empty($Email))
{
 $errors[]='Email address';
}

// Email verification amended from http://www.plus2net.com/php_tutorial/php_email_validation.php

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email))
{
 $errors[]='This is not a valid email address';
}



if(strlen($Password1) < 6)
  	 {
  	  $errors[]="longer";
  	 }	

if(empty($Password1))
{
 $errors[]='No Password';
}

	if($Password1 != $Password2)
		{
  	 	 $errors[]='Do not Match';
  	 	}
  	 	 
  	 	

if (count($errors)==0)
{
  // Connect to mysql 

  $dbServer=mysql_connect("localhost","0274148","8lgn62");

    if (!$dbServer) 
    {echo "Failed to connect to MySQL"; 
     exit;
    }
    else 
    {
     echo"connected to the database";
    }
    //Checks the database for existing records before writing.Username is primary key and Email must be unique.
    mysql_select_db("db0274148",$dbServer);
    $sql=("SELECT * from users WHERE name= '$User' AND name= '$Email'");
    
    $queryResult=mysql_query($sql);

    if ($queryResult==0)
    {
    // Inserts the data from the register page into the database

    mysql_select_db("db0274148",$dbServer);
    mysql_query("INSERT into users (Firstname, Surname, Email, Dob, Password, Username)
                        VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') ") 
                        or die(mysql_error());
            }
            	else
            	{
            	 echo"Sorry that username has already been chosen";
            	 // Return to the form
            	}
            // will show if there has been an error and tell what error it is
            if (mysql_error())
             {
              print ("There has been an error<BR>".mysql_error());
              exit;
             }
 }

	else
	{
	 echo'The following errors were found:<ul>';
	 	foreach($errors as $error)
	 	 {
	 	   echo "<li>$error</li><br>";
	 	 }

	 echo'</ul>';
	 include("registerform.html");
	 }

?>
   

Link to comment
Share on other sites

have tried that Scott but now get another error

 

Notice: Undefined variable: query in /home/stud/0/0274148/public_html/Assignment/registerdetails.php on line 95

 

which seems to relate to the first bit of code. Is this an array and would require defining ??

 

Cheers

 

Sudsy

Link to comment
Share on other sites

sorry to use to my own code

replace

mysql_query("INSERT into users (Firstname, Surname, Email, Dob, Password, Username)
                        VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') ")
                        or die(mysql_error()."<br>{$query}");

with

$sql="INSERT into users (Firstname, Surname, Email, Dob, Password, Username)
                        VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') ";
mysql_query($sql) or die(mysql_error()."<br>{$sql}")

 

Scott.

Link to comment
Share on other sites

ok that seems to have rid of most of the errors, sorry to be a pest but now how else do i call this sql statement if this isn't correct ?

 

I have also tried naming them the records that they would be stored under in the database i.e User and Email but no change.

 

Unknown column 'name' in 'where clause'

SELECT * from users WHERE name="$User" AND name="$Email"

Link to comment
Share on other sites

here you go mate i am lost now lol, am i right in thinking that should be a valid sql query ?

 

 

<? session_start();

$Firstname = $_POST['Firstname'];
$Surname = $_POST['Surname'];
$Email = $_POST['Email'];
$Dob = $_POST['Dob'];
$Password1 = $_POST['Password1'];
$Password2 = $_POST['Password2'];
$User = $_POST['User'];
$errors = array();


if(empty($Firstname))
{
 $errors[]='Please enter your Firstname';
}

if(empty($Surname))
{
 $errors[]='Please enter your Surname';
}

if(empty($User))
{
 $errors[]='You must select a Username';
}

if(empty($Dob))
{
 $errors[]='Date of Birth';
}
	/*elseif(!is_numeric($Dob))
	{
	 $errors[]='Please enter your Date of Birth using numbers...I.E 15/09/2000<br>';
	}*/

if(empty($Email))
{
 $errors[]='Email address';
}

// Email verification amended from http://www.plus2net.com/php_tutorial/php_email_validation.php

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email))
{
 $errors[]='This is not a valid email address';
}



if(strlen($Password1) < 6)
  	 {
  	  $errors[]="longer";
  	 }	

if(empty($Password1))
{
 $errors[]='No Password';
}

	if($Password1 != $Password2)
		{
  	 	 $errors[]='Do not Match';
  	 	}
  	 	 
  	 	

if (count($errors)==0)
{
  // Connect to mysql 

  $dbServer=mysql_connect("localhost","0274148","8lgn62");

    if (!$dbServer) 
    {echo "Failed to connect to MySQL"; 
     exit;
    }
    else 
    {
     echo"connected to the database";
    }
    //Checks the database for existing records before writing.Username is primary key and Email must be unique.
    mysql_select_db("db0274148",$dbServer);
    $sql=("SELECT * from users WHERE name='$User' AND name='$Email'");
    
    $queryResult=mysql_query($sql);

    if ($queryResult==0)
    {
    // Inserts the data from the register page into the database

    mysql_select_db("db0274148",$dbServer);
    mysql_query("INSERT into users (Firstname, Surname, Email, Dob, Password, Username)
                        VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') ");
                        mysql_query($sql) or die(mysql_error()."<br>{$sql}");
            }
            	else
            	{
            	 $errors[]='Sorry that username has already been chosen';
            	 // Return to the form
            	}
            // will show if there has been an error and tell what error it is
            if (mysql_error())
             {
              print ("There has been an error<BR>".mysql_error());
              exit;
             }
 }

	else
	{
	 echo'The following errors were found:<ul>';
	 	foreach($errors as $error)
	 	 {
	 	   echo "<li>$error</li><br>";
	 	 }

	 echo'</ul>';
	 include("registerform.html");
	 }

?>
   

Link to comment
Share on other sites

ok i made some changes and you only need to select the db once

and what i meant before was to replace the whole query with my code but i have done that for you.

<?php
session_start();

$Firstname = $_POST['Firstname'];
$Surname = $_POST['Surname'];
$Email = $_POST['Email'];
$Dob = $_POST['Dob'];
$Password1 = $_POST['Password1'];
$Password2 = $_POST['Password2'];
$User = $_POST['User'];
$errors = array();


if (empty($Firstname)) {
$errors[] = 'Please enter your Firstname';
}

if (empty($Surname)) {
$errors[] = 'Please enter your Surname';
}

if (empty($User)) {
$errors[] = 'You must select a Username';
}

if (empty($Dob)) {
$errors[] = 'Date of Birth';
}
/*elseif(!is_numeric($Dob))
{
$errors[]='Please enter your Date of Birth using numbers...I.E 15/09/2000<br>';
}*/

if (empty($Email)) {
$errors[] = 'Email address';
}

// Email verification amended from http://www.plus2net.com/php_tutorial/php_email_validation.php

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {
$errors[] = 'This is not a valid email address';
}

if (strlen($Password1) < 6) {
$errors[] = "longer";
}

if (empty($Password1)) {
$errors[] = 'No Password';
}

if ($Password1 != $Password2) {
$errors[] = 'Do not Match';
}


if (count($errors) == 0) {
// Connect to mysql

$dbServer = mysql_connect("localhost", "0274148", "8lgn62");
mysql_select_db("db0274148", $dbServer);

if (!$dbServer) {
	echo "Failed to connect to MySQL";
	exit;
} else {
	echo "connected to the database";
}
//Checks the database for existing records before writing.Username is primary key and Email must be unique.

$sql = ("SELECT * from users WHERE name='$User' AND name='$Email'");

$queryResult = mysql_query($sql) or die(mysql_error() . "<br>{$sql}");

if ($queryResult == 0) {
	// Inserts the data from the register page into the database

	$sql = "INSERT into users (Firstname, Surname, Email, Dob, Password, Username)
                        VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') ";
	mysql_query($sql) or die(mysql_error() . "<br>{$sql}");
} else {
	$errors[] = 'Sorry that username has already been chosen';
	// Return to the form
}
// will show if there has been an error and tell what error it is
if (mysql_error()) {
	print ("There has been an error<BR>" . mysql_error());
	exit;
}
} else {
echo 'The following errors were found:<ul>';
foreach ($errors as $error) {
	echo "<li>$error</li><br>";
}

echo '</ul>';
include ("registerform.html");
}
?>

 

Scott.

Link to comment
Share on other sites

in the first query where you try to check for existing username it is saying there is no column called name

and taking a closer look at the query it will fail because you are saying you want `name` to equal $user and $email at the same time which is imposable

SELECT * from users WHERE name='$User' AND name='$Email'

should this look like

SELECT * from users WHERE name='$User'

because other wise you could get people with the same username but different emails

 

Scott.

Link to comment
Share on other sites

Scott,

 

the problem i am getting here is the name part of the sql statement

 

SELECT * from users WHERE name='$User'

 

the error code i think isn't accepting this as i always get

 

Unknown column 'name' in 'where clause'

SELECT * from users WHERE name='A.Suddes@wlv.ac.uk' or

 

Unknown column 'name' in 'where clause'

SELECT * from users WHERE name='sudsy'

 

Link to comment
Share on other sites

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.