insert data to db after vlaidation

Hello ,

Im working in a php form where Im validating the feilds with regular expression. Validation working fine but I want to insert data into database after the validation finish with no error … I dont know how to rite this ?

Im facing a prolem and data is inserting empty records into database each time I click submit. Also , I want after the insert is done to redirect to a Thank you page

here is my code plz help

[php]<?php
$errname = “”;
$errage = “”;
$errmobile = “”;

  if($_POST["ac"]=="login"){
    // Full Name must be letters, dash and spaces only
      if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["name"]) === 0)
      $errname = '<p class="errText">Please enter your full name </p>';
    // age must be 2 digits
    if(preg_match("/^\d{2}$/", $_POST["age"]) === 0)
      $errage = '<p class="errText">Age must be 2 digits</p>';
	          // Mobile mask 050-0000000
    if(preg_match("/^\d{3}-\d{7}$/", $_POST["mobile"]) === 0)
      $errmobile = '<p class="errText">Mobile must be in this format: 050-0000000</p>';


	  // contact to database

$connect = mysql_connect(“localhost”, “root”, “”) or die (“Error , check your server connection.”);

mysql_select_db(“career_system”);

//Get data in local variable

$v_name=$_POST[‘name’];

$v_age=$_POST[‘age’];

$v_gender=$_POST[‘gender’];

$v_mobile =$_POST[‘mobile’];

$query=“insert into applicant(name, age, gender, mobile ) values (’$v_name’, ‘$v_age’, ‘$v_gender’,’$v_mobile ')”;

mysql_query($query) or die(mysql_error());
}
echo “Your information has been received”;
}

?>

 

Full Name "> <?php if(isset($errname)) echo $errname; ?>
Age "> <?php if(isset($errage)) echo $errage; ?>
Gender Male Female  
Mobile "> <?php if(isset($errmobile)) echo $errmobile; ?>
   
[/php]
Hi please do the below modification in your code. Use this code [php] //Use this code if($errname == '' && $errage == '' && $errmobile == '') { $query="insert into applicant(name, age, gender, mobile ) values ('$v_name', '$v_age', '$v_gender','$v_mobile ')"; mysql_query($query) or die(mysql_error()); } [/php]

Instead of below code
[php]
//Instead of below code
$query=“insert into applicant(name, age, gender, mobile ) values (’$v_name’, ‘$v_age’, ‘$v_gender’,’$v_mobile ')”;
mysql_query($query) or die(mysql_error());
[/php]

Other than everything is fine.
I hope this will helpful for you.
reply your feedback.
~SR~

Thx for ur reply Sarthak Patel

I tried it and it is not inserting but the proplem is when I enter a valid name and rest fields invalid data and click submit , it remove the name and I have to enter it again …

I dont want user to reenter the data again when it is valid , I want only the user to correct the invalid data and re enter them.

Thank u soo much really I need to finish this soon

I solved it :slight_smile:

Im sending just in case some 1 face same problem:
[php]if(preg_match("/^[A-Z][a-zA-Z ]+$|\p{Arabic}/u", $_POST[“name”]) === 0) [/php]

hello :D :) it's really good to hear that your problem is resolve. Enjoy.. ~~SR~~
Sponsor our Newsletter | Privacy Policy | Terms of Service