Jump to content

Help with editing specific data in a table entered in a form


Darkmatter5

Recommended Posts

I have an edit form I'm creating that has, for example 2 Text Boxes to input the data to edit and a submit button at the bottom to store/update the data in the table.  My problem is how would I go about writing a dynamic command to change only the fields in the table that correspond to the Text Boxes that had data entered.

 

Example:

 

DATA IN TABLE

FirstName=Mary

LastName=Smith

 

Say for instance Mary got married and changed her last name to Doe.

 

FORM FOR EDITING THE TABLE

Firstname: *text box*

Lastname: *text box*

[submit] <-submit button

 

Now if the lastname is the only item I need to change in the table how do I go about only changing the lastname in the table, but for future instances be able to change both fields or only the firstname field without having to write some huge if statement with each possible instance?

 

I currently useto update everything, but I think it'll overwrite data in the table to NULL even if something isn't entered in a particular text box.  Which can cause problems if data in the table is already entered and write, it would then be changed to NULL.

 

$query = "INSERT INTO clients (FirstName, LastName) VALUES )'$FirstName','$LastName')";

mysql_query($query) or die('Error, insert query failed');

 

How do I use basically the same code to only change the required fields.

 

Thanks,

 

 

What you could do is put a checkbox next to the fields you want to edit, that way you can just select those fields for edit. Let's say we name the checkboxes fields. Im your loop you can put in

<input type="checkbox" name=fields[] value="<?php echo $row['fieldname']; ?>" />

 

field name would be the name of the field.

 

then you could do this to get the query

$fields = implode(", ", $_POST['fields'];
$sql = "SELECT $fields FROM table WHERE id = '$id'";

 

As I said i don't know your table structure so just giving example

 

if you give me some code for your list and form I can help out

 

Ray

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.