Jump to content

Using PHP to fill editable fields on a form from MYSQL


bnther

Recommended Posts

I'm a good way through building my 'membership form'. (At least I think I am) I have written a form that will allow me to add to a database and even included a little validation. I can sort and output the database and everything is moving along. What I don't know how to do is bring the info back into a form that I can edit. For example, if a member changes their phone#, as it stands right now, I'd have to change it directly in phpMyAdmin. The LAST thing I want is for my client figure out where that is!

 

So how do I fill a form with values from a database where I can later hit 'update'?

Links to tutorials would be greatly appreciated.

Link to comment
Share on other sites

Let's assume you want to change the member's phone number.

 

The first thing you'd need to do is use a MySQL select statement to get the data you need from the database.

 

This assumes that the member's table includes a column named "id" that holds a unique id that's associated with each member. You'll need to know this id ahead of time somehow (one way to do this is to pass the value through the url. For example, if the url was "yoursite.com/file.php?id=3" you could use $_GET['id'] to get the value from the url.

 

Maybe something like this:

 

$id =  $_GET['id']; // this is just a very basic example, you would want to validate this to ensure that it is numeric otherwise you'll run into issues
$result = mysql_query("SELECT phonenumber FROM members WHERE id=$id"); 

 

And then you would need to check if there are results, and if so, set a variable:

 

$row = mysql_fetch_array($result);
if($row == true) // check that the 'id' matches up with a row in the database
{
   $phonenumber = $row['phonenumber'];
}

 

And then in your form you could set an input's value like this:

 


 

if you need the value to go into a textarea instead, you'd use this:

 


 

I have a tutorial on this site that partially covers this:

http://www.killersites.com/forums/topic/2282/basic-php-system-vieweditdeleteadd-records/

Link to comment
Share on other sites

I'm a good way through building my 'membership form'. (At least I think I am) I have written a form that will allow me to add to a database and even included a little validation. I can sort and output the database and everything is moving along. What I don't know how to do is bring the info back into a form that I can edit. For example, if a member changes their phone#, as it stands right now, I'd have to change it directly in phpMyAdmin. The LAST thing I want is for my client figure out where that is!

 

So how do I fill a form with values from a database where I can later hit 'update'?

Links to tutorials would be greatly appreciated.

 

 

I'd invest in a book covering the process of webapplication with PHP+mySQL or go and take some classes if I were you. Just hearing you saying client and not knowing these basic steps gives me chills. Have you informed your client that you are a beginner when it comes to this?

Have you any ideas how vulnerable your code can be if you do not have any experience with online security and are creating code accepting user input?

 

Last thing anyone wants is a big disaster that could very well lead to the client sueing you or spread unwanted publicity that will haunt you for years. The second outcome would be the best thing to happen in that case.

Link to comment
Share on other sites

Thanks for the reply.

 

Also, thanks for the tutorials. I've used them quite a bit and they've been a big help. :D

 

I'll work a little with the code that you presented in this post. At glance, it seems to make pretty easy sense. Same thing, just different angle

 

Thanks again for the help.

Link to comment
Share on other sites

Just hearing you saying client and not knowing these basic steps gives me chills. Have you informed your client that you are a beginner when it comes to this?

 

Have you any ideas how vulnerable your code can be if you do not have any experience with online security and are creating code accepting user input?

 

 

Thanks for the reply.

 

THE CHILLS ?! :P

 

LOL

 

The work is for a local chamber of commerce and it is for free (yuck). I really don't like doing free work, not because I'm greedy...OK maybe not JUST because I'm greedy, but I've found that people really don't appreciate it when they don't have a concept of the effort I'm putting in. In the very least, they seem less likely to give you 'little things' to work on if they know that I'll send them a bill for it.

 

That being said, I felt that this would be a good opportunity to build a little experience with something that it is new to me. I'm pretty much resolved that I will always be learning something in this profession.

 

As far as security, the form will not be linked to the website. The chamber secretary will need the actual address to access the form. There will be a user name and password to the form, as well as the database. It might not be bullet proof, but it's a place to start.

 

Thanks again for the reply.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...