Jump to content

a few tweaks to PHP/MySQL form I would like to make


webguync

Recommended Posts

I have a form which submits to a MySQL DB. There is validation in place so that all fields are required, and the form won't submit until the fields are filled in correctly and then submits. Everything works fine as far as the validation and submitting. A few tweaks, I would like to make are...

1) When you submit the form and there are validation errors, the info that was filled in correctly is wiped out. I would like the correct fields to stay intact so that say one field was not filled in or filled in correctly then the user wouldn't have to fill in all the fields all over again.

 

2) Currently after the user fills in their name, if the username is already taken, they get an error on the page "Error: Duplicate entry 'Bruce.Gilbert' for key 'usr'". This appears to be a built in MySQL function. The form dies not show at this point, even if you refresh the page. I would like the form to still display with the values intact if there is a duplicate entry is the Username field.

 

Here is the pertenant code.

 

<?PHP
require_once "formvalidator.php";
$show_form=true;
if(isset($_POST['Submit']))
{
   $validator = new FormValidator();
    $validator->addValidation("FirstName","req","Please fill in FirstName");
$validator->addValidation("LastName","req","Please fill in LastName");
$validator->addValidation("UserName","req","Please fill in UserName");
$validator->addValidation("Password","req","Please fill in a Password");
$validator->addValidation("Password2","req","Please re-enter your password");
$validator->addValidation("Password2","eqelmnt=Password","Your passwords do not match!");
    $validator->addValidation("email","email","The input for Email should be a valid email value");
    $validator->addValidation("email","req","Please fill in Email");
$validator->addValidation("Zip","req","Please fill in your Zip Code");
$validator->addValidation("Security","req","Please fill in your Security Question");
    if($validator->ValidateForm())
    {
        $con = mysql_connect("localhost","uname","pw") or die('Could not connect: ' . mysql_error());

mysql_select_db("DBName") or die(mysql_error());





$FirstName=mysql_real_escape_string($_POST['FirstName']); //This value has to be the same as in the HTML form file

$LastName=mysql_real_escape_string($_POST['LastName']); //This value has to be the same as in the HTML form file

$UserName=mysql_real_escape_string($_POST['UserName']); //This value has to be the same as in the HTML form file

$Password= md5($_POST['Password']); //This value has to be the same as in the HTML form file

$Password2= md5($_POST['Password2']); //This value has to be the same as in the HTML form file

$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file

$Zip=mysql_real_escape_string($_POST['Zip']); //This value has to be the same as in the HTML form file

$Birthday=mysql_real_escape_string($_POST['Birthday']); //This value has to be the same as in the HTML form file

$Security=mysql_real_escape_string($_POST['Security']); //This value has to be the same as in the HTML form file



$sql="INSERT INTO Profile (`FirstName`,`LastName`,`Username`,`Password`,`Password2`,`email`,`Zip`,`Birthday`,`Security`) VALUES ('$FirstName','$LastName','$UserName','$Password','$Password2','$email','$Zip','$Birthday','$Security')"; 

if (!mysql_query($sql,$con)) {

die('Error: ' . mysql_error());

}



else{



mail('email@gmail.com','A profile has been submitted!',$FirstName.' has submitted their profile',$body);

echo "<h3>Your profile information has been submitted successfully.</h3>";

echo "<p>You may now <a href='login.php'>login</a></p>";



}

mysql_close($con);

        $show_form=false;
    }
    else
    {
        echo "<h3 class='ErrorTitle'>Validation Errors:</h3>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
            echo "<p class='errors'>$inpname : $inp_err</p>\n";
        }        
    }
}

if(true == $show_form)
{
?>

<form name="test" id="ContactForm" method="POST" action="" accept-charset="UTF-8">
<fieldset>

               <div class='normal_field'><label for="FirstName">First Name</label></div>
               <div class='element_label'>
                
                  <input type='text' name='FirstName' size='20'>
               </div>
            
           
               <div class='normal_field'><label for="LastName">Last Name</label></div>
               <div class='element_label'>
                  <input type='text' name='LastName' size='20'>
               </div>
           
           </fieldset>
           <fieldset>
               <div class='normal_field'><label for="UserName">User Name</label></div>
               <div class='element_label'>
                  <input type='text' name='UserName' size='20'>
               </div>
           
            
               <div class='normal_field'><label for="Password">Password</label></div>
               <div class='element_label'>
                  <input type='password' name='Password' size='20'>
               </div>
               <div class='normal_field'><label for="Password2">Re-Enter Password</label></div>
               <div class='element_label'>
                  <input type='password' name='Password2' size='20'>
               </div>
            
           
               <div class='normal_field'><label for="Email">Email</label></div>
               <div class='element_label'>
                  <input type='text' name='email' size='20'>
               </div>
            </fieldset>
            <fieldset>
          
               <div class='normal_field'><label for="Zip">Zip Code</label></div>
               <div class='element_label'>
                  <input type='text' name='Zip' size='20'>
               </div>
            
            
               <div class='normal_field'><label for="Birthday">Birthday(mm/dd/yyyy format)</label></div>
               <div class='element_label'>
                  <input type='text' name='Birthday' size='20'>
               </div>
           
           
            
               <div class='normal_field'><label for="Security">Security Question</label></div>
               <div class='element_label'>
                  <input type='text' name='Security' size='20'>
               </div>
               </fieldset>
               <div id="agree">
	                	<label for="tos">
	                		<input type="checkbox" id="tos" name="tos" value="yes" />
	                		I have read and agree to the <a href="ajax/serviceterms.html" id="terms">Terms of Service</a>.
	                	</label>
	                </div>
         <fieldset>
           <div id="service-terms" class="box rounded-all"></div>
	                
		                <div class="controls">
		                    <input id="submit" type="submit" name="Submit" value="CREATE PROFILE"/>	
		                </div>
	                </fieldset>
         

</form>
<?PHP
}//true == $show_form
?>

Link to comment
Share on other sites

to get round you duplicate key issue:

 

$sql="select * from Profile where username = '$username';

$result = mysql_query( $sql, $conn )

or die( "ERR: SQL 1" );

if(mysql_num_rows($result)!=0)

{

#do insert sql

}

else

{

#go here via header or display own message etc

}

 

hope that helps

 

Link to comment
Share on other sites

I will try that, but not sure where in my current code I need to place it since I am already doing an if statement for the validation. If no validation errors Do the SQL INSERT.

 

 

 

<?PHP
require_once "formvalidator.php";
$show_form=true;
if(isset($_POST['Submit']))
{
   $validator = new FormValidator();
    $validator->addValidation("FirstName","req","Please fill in FirstName");
$validator->addValidation("LastName","req","Please fill in LastName");
$validator->addValidation("UserName","req","Please fill in UserName");
$validator->addValidation("Password","req","Please fill in a Password");
$validator->addValidation("Password2","req","Please re-enter your password");
$validator->addValidation("Password2","eqelmnt=Password","Your passwords do not match!");
    $validator->addValidation("email","email","The input for Email should be a valid email value");
    $validator->addValidation("email","req","Please fill in Email");
$validator->addValidation("Zip","req","Please fill in your Zip Code");
$validator->addValidation("Security","req","Please fill in your Security Question");
$validator->addValidation("Security2","req","Please fill in your Security Answer");

    if($validator->ValidateForm())
    {
        $con = mysql_connect("localhost","uname","Jpw") or die('Could not connect: ' . mysql_error());

        mysql_select_db("DBName") or die(mysql_error());





$FirstName=mysql_real_escape_string($_POST['FirstName']); //This value has to be the same as in the HTML form file

$LastName=mysql_real_escape_string($_POST['LastName']); //This value has to be the same as in the HTML form file

$UserName=mysql_real_escape_string($_POST['UserName']); //This value has to be the same as in the HTML form file

$Password= md5($_POST['Password']); //This value has to be the same as in the HTML form file

$Password2= md5($_POST['Password2']); //This value has to be the same as in the HTML form file

$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file

$Zip=mysql_real_escape_string($_POST['Zip']); //This value has to be the same as in the HTML form file

$Birthday=mysql_real_escape_string($_POST['Birthday']); //This value has to be the same as in the HTML form file

$Security=mysql_real_escape_string($_POST['Security']); //This value has to be the same as in the HTML form file

$Security2=mysql_real_escape_string($_POST['Security2']); //This value has to be the same as in the HTML form file



$sql="INSERT INTO Profile (`FirstName`,`LastName`,`Username`,`Password`,`Password2`,`email`,`Zip`,`Birthday`,`Security`,`Security2`) VALUES ('$FirstName','$LastName','$UserName','$Password','$Password2','$email','$Zip','$Birthday','$Security','$Security2')"; 
//echo $sql;

if (!mysql_query($sql,$con)) {

die('Error: ' . mysql_error());

}



else{



mail('email@gmail.com','A profile has been submitted!',$FirstName.' has submitted their profile',$body);

echo "<h3>Your profile information has been submitted successfully.</h3>";

echo "<p>You may now <a href='login.php'>login</a></p>";



}

mysql_close($con);

        $show_form=false;
    }
    else
    {
        echo "<h3 class='ErrorTitle'>Validation Errors:</h3>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
            echo "<p class='errors'>$inpname : $inp_err</p>\n";
        }        
    }
}

if(true == $show_form)
{
?>

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.