PHP - inserting values in database and maintaining session

sanjay1911
sanjay1911 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Your 3rd line:
if (isset($_POST['submit']))

Open in new window

should actually be:
if (isset($_POST['Submit']))

Open in new window

as the name of your submit input is "Submit", not "submit"

Author

Commented:
@jordan : thankyou

The validations are working fine. However, the values are not getting inserted in the database.
Syntax error near line 76 with your insert into mysql syntax:
see:
VALUES ('',$fname',

Open in new window

should be:
VALUES ('','$fname',

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

...sorry - to be clear, that's line 80 in the code you provided.

Author

Commented:
The structure of database is like this: ( ' count' auto_increment, 'fname', 'lname', 'address', 'phone' and so on.

So, should I leave the auto_increment field blank??
Yeah, just leave it the way you have it... you just missed a " ' " on your $fname value is all...

Line 80 of your code you provided should be this:
$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')";

Open in new window

Author

Commented:
I have two tables in my website, one table holds registration and login details. primary key is uid(userid).

The second table is createprofile, this table is intended for storing the user details. here auto_increment is count.

how can I relate this two tables? I mean, every user has a unique id(uid). When he creates a profile on the webiste, his details should get saved. How can I relate uid with createprofile table?
I believe what you're looking for is:
$userid = mysql_insert_id(); 

Open in new window

When you insert a new row in a table, mysql_insert_id() returns the most recently created row's id...

Is that what you mean?

Author

Commented:
Thanks! the insert query worked. Now, the data is going in the database.

In createprofie.php page, if you look at the line  86 to 96 , even though I have created an associative array and stored user details in sessions, I am not able to retrieve that data. How can I retrieve session data?





You need to include
session_start();

Open in new window

at the top of every page that is connected to a page you're leaving from/going to.

Author

Commented:
What I am trying to do here is, maintain user data. I have created a createprofile.php page and create profile table in mysql which stores user data.

What I want is , once user has submitted his details in the database, the next time when user logs in, he should be able to see his data again and make appropriate changes to them i.e. edit details, delete details.

Oh - you want the data to auto-populate in the form once the user has logged in?

Author

Commented:
I have included the following code on every page.

Session_start(); is included in the config file.


require_once('include/config.php');

Open in new window

Author

Commented:
Yes, that's correct. I want the data to auto-populate  once user has logged in.
Just add the session vars to the input values if you're keeping the data in the session vars... like so:
<input type="text" name="fname"  value="<?=$_SESSION["hobbies"]?>"/>

Open in new window

and for a textarea:
<textarea name="company" cols="35" rows="5"><?=$_SESSION["company"]?></textarea>

Open in new window



Author

Commented:
@jordanrynard : you are awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks so much!!!!! It worked. Thanks a lot!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial