Jump to content

reg from


freddyw

Recommended Posts

i have this registration code

 

<?php

error_reporting(0);

  if(isset($_POST['first_name'])){  
    $firstname =   pg_escape_string($_POST['firstname']);
    $lastname =    pg_escape_string($_POST['lastname']);
    $username =     pg_escape_string($_POST['username']);
    $password =     pg_escape_string($_POST['password']);
    $verify =       pg_escape_string($_POST['verify']);

}

if($password != $verify) {
     die ('Sorry, The passwords you entered didn\'t match<br>Please use your browsers back button to try again');
     }



if ( ( !$firstname) or (!$lastname) or (!$username) or (!$password))


{
$form = "Please fill the fields below.";
$form.="<form action=\"$self\"";
$form.="method=\"post\">First Name: ";
$form.="<input type =\"text\"name=\"firstname\"";
$form.="value=\"$firstname\"><br>Last Name: ";
$form.="<input type =\"text\"name=\"lastname\"";
$form.="value=\"$lastname\"><br>Username: ";
$form.="<input type =\"text\"name=\"username\"";
$form.="value=\"$username\"><br>Paswword: ";
$form.="<input type =\"text\"name=\"password\"";
$form.="value=\"$password\"><br>Verify Password: ";
$form.="<input type =\"text\"name=\"verify\"";
$form.="value=\"$verify\"><br>";
$form.="<input type =\"submit\" value =\"Submit\">";
$form.="</form>";
echo ( $form );
}

else

{

$conn = @mysql_connect( "localhost", "root", "")
	or die ("could not connect to mysql");
	$db = @mysql_select_db ( "site_table", $conn )
	or die ("could not select databse");
	$sql = "insert into users
	(fist_name,last_name,user_name,password) values
	(\"$firstname\",\"$lastname\",\"$username\",password(\"$password\") )";

	$result = @mysql_query ($sql, $conn)
	or die ("could not execute query");
	if ($result) {

		echo ("Welcome $firstame you are registered as $username");
	}


}


?>

 

eroor reporting is turned off becuasing its throwing undefined variable errors

 

the reason this bit of code reads like it does

$conn = @mysql_connect( "localhost", "root", "")

is because my db currently has no password (will be sorted before this goes online)

 

 

however, once the form is filled in it resets all the fields blank and no info is put into the database

 

any ideas?

Link to comment
https://forums.phpfreaks.com/topic/173045-reg-from/
Share on other sites

Turn error reporting back on and fix those undefined variable notices. All development should be done with error reporting set to E_ALL.

 

As for your code, you could at least attempt to debug it. Remove all error suppression for starters.

 

$result = mysql_query ($sql, $conn);
if ($result) {
  echo ("Welcome $firstame you are registered as $username");
} else {
  echo mysql_error(); // Just here for debugging. You might want to look into trigger_error() instead though.
}

Link to comment
https://forums.phpfreaks.com/topic/173045-reg-from/#findComment-912089
Share on other sites

One of the problems you will find when you debug your code is that $_POST['first_name'] in the following line of code will never be set because your form has no field named 'first_name' -

 

if(isset($_POST['first_name'])){  

 

You are also using the pg_escape_string() function without having a connection to a PostgreSQL database present which will probably result in NULL values out. And you are mixing that pg_ function with mysql_ functions.

 

Unfortunately, someone suggested in one of his other threads that changing the error_reporting setting to hide notices was OK rather than finding the reason for them and fixing the code.

 

By hiding all the notice/warning/error's it will make it impossible to find real problems in your code. Your code won't work and you won't have any indication why, such as a typo in a variable name...

Link to comment
https://forums.phpfreaks.com/topic/173045-reg-from/#findComment-912091
Share on other sites

so PFM would this make more sence

 

<?php
$self = $_SERVER ['PHP_SELF']
$firstname = $_POST ['firstname';]
$lastname = $_POST ['lastname';]
etc etc
?>

to start the code rather than what i have

 

 

and thorpe, can you give me a clue on how to define my variables... im recieving these...

 

Notice: Undefined variable: verify in C:\wamp\www\register2.php on line 15

Notice: Undefined variable: password in C:\wamp\www\register2.php on line 15

Notice: Undefined variable: firstname in C:\wamp\www\register2.php on line 21

Notice: Undefined variable: self in C:\wamp\www\register2.php on line 26

Notice: Undefined variable: firstname in C:\wamp\www\register2.php on line 29

Notice: Undefined variable: lastname in C:\wamp\www\register2.php on line 31

Notice: Undefined variable: username in C:\wamp\www\register2.php on line 33

Notice: Undefined variable: password in C:\wamp\www\register2.php on line 35

Notice: Undefined variable: verify in C:\wamp\www\register2.php on line 37

 

 

Link to comment
https://forums.phpfreaks.com/topic/173045-reg-from/#findComment-912093
Share on other sites

Most of the undefined error messages are because the logic is incorrect. The code on the page to process the form should only be executed when the form is submitted. You have some of it inside of an if() {} conditional statement (which is correct) and some of it outside of that conditional statement. The code outside of the conditional statement is executed every time the page is requested, not just when the form has been submitted, giving the undefined error messages when the nonexistent variables are accessed.

 

In general, you need to execute the following logic to have a form and form processing code on the same page -

 

<?php
// basic form and form processing code on one page

// condition the form variables and setup default values -
$submitted = isset($_POST['submit']) ? $_POST['submit'] : FALSE; // was the form submitted?
$name_field = isset($_POST['name']) ? $_POST['name'] : ""; // condition the form's name field
// add more form fields here - only actual variables that are expected are set to prevent injection attempts

// The form processing code -
if($submitted) {
$form_error = array(); // array to hold any form validation errors

// validate the form data here (set elements in $form_error to hold error messages)
if(empty($name_field)) {
	$form_error['name_field'] = "Please fill in the name";
}
// more validation code for the rest of the form data...

// if there were no form validation errors, use the data that was submitted
if(empty($form_error)) {
	// do something with the data here
	echo "The name you entered was: $name_field<br />";
}
}

// The form code - display the form if it has not been submitted or there are form validation errors
If(!$submitted || !empty($form_error)) {
$error_message = "";
// check for and display any form validation errors
if(!empty($form_error)) {
	$error_message = "Please correct the errors shown -";
}

// display the form, with any errors and previously submitted values
?>
<?php echo $error_message; ?>
<form method="post" action="">
<label for="name">Name: <?php echo isset($form_error['name_field']) ? $form_error['name_field'] : "Required";?></label><br />
<input type="text" name="name" id="name" value="<?php echo $name_field; ?>">
<input type="submit" name="submit">
</form>
<?php
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/173045-reg-from/#findComment-912371
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.