troubleshooting Question

PHP - inserting values in database and maintaining session

Avatar of sanjay1911
sanjay1911 asked on
PHP
16 Comments1 Solution419 ViewsLast Modified:
Hello,

I have created a form, this purpose of this form is to store user data in the database. Also, it should store user data in sessions. However, when I click on submit button, nothing happens. Data is not getting inserted into database.

Here is my code for create CV page.
<?php 
require_once('include/config.php');

	if (isset($_POST['submit']))
	{
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$address = $_POST['address'];
	$phone = $_POST['phone'];
	$age = $_POST['age'];	
	$school = $_POST['school'];
	$college = $_POST['college'];
	$university = $_POST['university'];
	$company = $_POST['company'];
	$personality = $_POST['personality'];
	$hobbies = $_POST['hobbies'];
	
if ( (!empty($_POST["fname"])) && (!empty($_POST["lname"])) && (!empty($_POST["address"])) && (!empty($_POST["phone"])) && (!empty($_POST["age"])) && (!empty($_POST["school"])) && (!empty($_POST["college"])) && (!empty($_POST["university"])) && (!empty($_POST["company"])) && (!empty($_POST["personality"]))  && (!empty($_POST["hobbies"])))
{
    // Yes, the posted data is available. Escape it for use in query
	$fname = mysql_real_escape_string($_POST["fname"]);
    $lname = mysql_real_escape_string($_POST["lname"]);
    $address = mysql_real_escape_string($_POST["address"]);
    $phone = mysql_real_escape_string($_POST["phone"]);
	$age = mysql_real_escape_string($_POST["age"]);
	$school = mysql_real_escape_string($_POST["school"]);
	$college = mysql_real_escape_string($_POST["college"]);
	 $university = mysql_real_escape_string($_POST["university"]);
	 $company = mysql_real_escape_string($_POST["company"]);
	$personality = mysql_real_escape_string($_POST["personality"]);
	$hobbies = mysql_real_escape_string($_POST["hobbies"]);
}
	
	
$errors=array();
if(!preg_match('/[A-Za-z]/', $fname))
	$errors[]="Name is not valid.";
if(!preg_match('/[A-Za-z]/', $lname))
	$errors[]="Name is not valid.";
if(empty($fname))
	$errors[]="Please enter your first name.";
	if(empty($lname))
	$errors[]="Please enter your last name.";
if(empty($address))
	$errors[]="Please enter your address.";
if(empty($phone))
	$errors[]="Please enter your phone number.";
if(!preg_match('/[0-9]/', $phone))
	$errors[]="Phone number is not valid.";
if(empty($age))
	$errors[]="Please enter your age.";
if(!preg_match('/[0-9]/', $age))
	$errors[]="Age is not valid.";
if(empty($school))
    $errors[]="Please enter your schooling details.";
if(empty($college))
	$errors[]="Please enter your College details.";
if(empty($university))
	$errors[]="Please enter your University details.";
	if(empty($company))
	$errors[]="Please enter your work experience.";
	if(empty($personality))
	$errors[]="Please provide informaiton about your personality traits.";
	if(empty($hobbies))
	$errors[]="Please provide information about your hobbies.";
	
  // $res=mysql_query($sql) or die(mysql_error());

if(sizeof($errors)>0){
	echo ("<font color='red'>");
	foreach ($errors as $err){
	echo ($err."<br/>");
	}
	echo ("</font>");
	exit();
}else{
        // Make the unique user key
      //  $uuk = md5($uid . $pwd . rand());
	  
        $sql = "INSERT INTO createprofile (count,fname, lname, address, phone, age, school, college, university,company, personality, hobbies) VALUES ('',$fname','$lname', '$address', '$phone', '$age','school','$college','$university','$company', '$personality', '$hobbies')";
       $resu=mysql_query($sql);

        
		
		// store user details in session array
        $_SESSION["fname"] = $fname;
		$_SESSION["lname"] = $lname;
		$_SESSION["address"] = $address;
		$_SESSION["phone"] = $phone;
		$_SESSION["age"] = $age;
		$_SESSION["school"] = $school;
		$_SESSION["college"] = $college;
		$_SESSION["university"] = $university;
		$_SESSION["company"] = $company;
		$_SESSION["personality"] = $personality;
		$_SESSION["hobbies"] = $hobbies;

       
        // If create cv form is sucessfull, redirect the user to login_main page
echo "<meta http-equiv='refresh' content='0;url=\"99home.php?page=login_main\"'>";
			exit;
    }

}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create Profile</title>

</head>
<div>
<h3> PERSONAL DETAILS</h3>
  <form  name="createprofile" method="post" >
    
<p>Firstname:<input type="text" name="fname"  />
<p>Lastname:<input type="text" name="lname"  />
<p>Address: <textarea name="address" cols="45" rows="5"></textarea>
<p>Phone: &nbsp;&nbsp;&nbsp;<input type="text" name="phone"  />

<p>Age &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="age" /><br/>

<h3> Education Qualification</h3><br/>
<p>School:&nbsp;&nbsp;<textarea name="school" cols="45" rows="5"></textarea>
<p>College:&nbsp;&nbsp;<textarea name="college" cols="45" rows="5"></textarea>
<p>University:&nbsp;&nbsp;<textarea name="university" cols="43" rows="5"></textarea>


<p><h3> Work Experience</h3>
<p>Company Details:&nbsp;&nbsp;<textarea name="company" cols="35" rows="5"></textarea>

<p><h3> Personality Traits</h3>
<p>Personality Traits:&nbsp;&nbsp;&nbsp;<textarea name="personality" cols="35" rows="5"></textarea>

<p><h3>HOBBIES</h3>
<p>HOBBIES:&nbsp;&nbsp;<textarea name="hobbies" cols="45" rows="5"></textarea>


<p><input type="submit" name="Submit"  value="Submit" />
    
    
  </form>
</div>


</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
jordanrynard

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros