Jump to content

Autofill form with Database info


Xeven

Recommended Posts

Ok so I have a form that is working all fine.  But now im getting into the "fine tuning" stuff and want to make it a lot more user friendly.

 

Basically this form is going to be used to update the logged in users details.  So there will be four fields viewable: Firstname, Lastname, Password and Confirm Password.

 

What I am trying to get happening is, when the page is loaded I would like the Firstname and Lastname to be populated with the user's details.  Now because there is a session being held on the page it should be possible to pull this data from the database right?

 

This is what my code looks like, I don't seem to get any errors, but the fields just don't populate

 

      <p><form id="cpupdate" name="update" method="POST" action="update.php">
	 <?php
	 include ("info.inc");

	 $connect = mysql_connect($host,$account,$password);
         $db = mysql_select_db("database",$connect)
         or die("Can't connect to database");

	 $sql = "SELECT * FROM user WHERE user_id = '".$_SESSION['sessioname']."'"; 
	 $result = mysql_query($sql,$connect) or die (mysql_error());

	 $row=mysql_fetch_array($result);
	 {
	 $firstname = $row['firstName'];
	 $lastname = $row['lastName'];
	 }
	 ?>
        <label>

        <div align="left">First Name</div>          
          <div align="left">
	  <input type="text" value="<?php echo $firstname;?>" maxlength="25" name="firstname" id="firstname" 
			title="Enter your first name"/>

 

I really hope I am not overlooking something that is pretty obvious.  I thought I was just about getting the hang of this.

 

Thanks

Link to comment
Share on other sites

Couple of checks

 

In your mysql, are the field names firstName and lastName, with capital N's (not sure if this makes a difference)

For

$sql = "SELECT * FROM user WHERE user_id = '".$_SESSION['sessioname']."'";

Try

$sql = "SELECT * FROM `user` WHERE `user_id` = '{$_SESSION['sessioname']}'";

 

Just in case something is wrong with the query (although it looked okay)

 

print $_SESSION['sessioname']; for me

 

Just noticed this!!! Should that be sessioName or sessioNName? How many N's?

Link to comment
Share on other sites

Hey guys, thanks for all your help on this.  It all helped and made me realise where I had gone wrong.  It must have been pretty late last night.

 

Well the problem was right here

 

$sql = "SELECT * FROM user WHERE user_id = '".$_SESSION['sessioname']."'"; 

 

It wasn't supposed to be looking at the user_id but was supposed to be looking at another field called email!

 

I feel like such an idiot after finally working it out.

 

But thanks to all that helped, without it I probably would not have realised what I did wrong.

Link to comment
Share on other sites

Sometimes you just have to sit there and go through it literally word by word... if you just read it through you see what you want to see.

 

"Select the info, that's right, display it, that's right... so where did I go wrong"

 

Instead you might have to break problems down into

"Right, so I"

Query the database for x fields by y condition.

Do zz to the field

Print y field.

 

If you write it down, you might notice where it doesn't follow your plan (assuming you plan your functions, a basic layout of what you want to do with the function goes a long way)

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.