I need to be able to put data from a mysql table into a dynamic html table - that's not difficult but I need to also retrieve the column names, so for the data I can't use <?php echo $field; ?>. I can get all the data into a table using the following code:

But then I need to be able to edit the items in the table or to delete any of them or add another. I am not sure how to do this without any reference to the item since I won't know what the fields are. Can anyone help please?thanksP.S. I'm not allowed (by the spec) to use javascript

djjjozsi
—
2009-12-01T16:26:23Z —
#2

before you run this query: select * from $table

create a function which collects field names and key types from your table:run a query: SHOW FIELDS FROM database.table and you will see how this query works.

and if KEY index is PRI, you can use that to create modify link, delete link.

juliamfg
—
2009-12-01T17:43:22Z —
#3

I have used that query - SHOW FIELDS FROM - but I am not quite sure how that will help me. The first field in the table is the primary key but how do I identify that for editing purposes? A user might want to edit only one of the fields with that primary key so how would I identify that and how would I make the mysql query to update thta in the table? This particular table is a list of products with description, price etc etc so a user may only want to change the descripton or the price, for instance.Sorry if I am being dense but I am learning PHP as I go along.thanks for any help

djjjozsi
—
2009-12-01T18:26:10Z —
#4

Lets learn the very basics how you can make fields for update.

First, list the table, build update link based on the primary key.

Watch the url, and check if $_GET["id"] has value,build a MYSQL select link with a condition.

You can find tutorial videos, articles around the internet.

juliamfg
—
2009-12-02T15:00:19Z —
#5

I do know how to insert mysql data into a html table and create the links to do updates and have done that loads of times but never when I did not know the field names first. I can get the primary id and all the other attributes of the field names but I still do not know how to make the link in the data rows to select a row to update or delete and in the case of update how would I do an update query when I do not know the names of the fields to update?If you could post a code sample to explain that would help.thanks

system
—
2009-12-02T15:23:08Z —
#6

Too many tasks here.Updating a row and building a table are different tasks. Please choose one.

If you want to update a row, you have to build an HTML form. And fill it with row data. And after submit you can build an update query and execute it.

juliamfg
—
2009-12-02T15:34:21Z —
#7

OK I should not have got carried away! If you see the code at the top of the thread, I can execute a query to retrieve both the field names and data from a table and insert it into a html table. What I cannot get my head around is how to assign variables in order to make a form to submit. I have got a potential edit link at the end of each row and would like to link that with the primary id which is always the first field in any of our tables. How do I select what to edit??

system
—
2009-12-02T15:39:14Z —
#8

You need another script for editing.You don't have to assign anything in this table. It will be pulled from database and assigned to input fields in that editing script.So, you ought to go for it. For editing script. You have to done it first.

Forget that table for a while

juliamfg
—
2009-12-02T15:50:01Z —
#9

Right I have retrieved both the field names and the data and sent it to a html table but since I do not know the names of the fields before this and essentially never know (in terms of writing the query) how can I make an input field for the edit? Any of the data in the rows, apart from the primary id, can potentially be edited.

djjjozsi
—
2009-12-02T16:18:40Z —
#10

A dynamic updater is looks like this. Its creating input fields.See, its not as easy as you think, but there are alternate ways for doing updaters. Learn the basics, write the field names into an array, and in a foreach loop you can make input fields. The shorter way is a bit simplier.From this code the pager, delete(batch delete) order , batch edit , search and insert features are missing.

Oh, I never thought it was easy! That's why I was having such a problem understanding how I should approach it. Thank-you so much for showing me, that code does what I want to do for the editing, now I can add the delete and add parts. Thank-you again, my php knowledge is growing!

djjjozsi
—
2009-12-02T16:56:14Z —
#12

When you finish with the code, search for: Ajax table editor for mysql That makes these tasks.

The problem with these programs is if you need more complex table structure.

juliamfg
—
2009-12-22T07:32:26Z —
#13

I am using the code you gave me to do the updates on my table and have incorporated it into my system, everything works fine. However, try as I might I cannot work out how to do a delete on a whole row. This can either be done in the same file as the update or in a separate one i.e the delete and update do not have to be there at the same time. I have tried both ways but I just can't get it to work. I also need to be able to ask the user to confirm that they want to delete a row before the mysql query is actioned. And re your last remark about Ajax - I wish I could use it but the spec I've been given is that NO javascript can be used - just to make things more difficult!Thanks for any help, hints you can give

djjjozsi
—
2009-12-22T10:24:54Z —
#14

i don't really like taking evry features into one big function, howeverdeleting an entry with a confirmation is not a big deal.

You know the primary key field, you can make checkboxes with array field names.

I understand how to do that but I am not sure how to incorporate it with the rest of the php I am using for the update - or would this go into a separate file for deleting? If so, the question again is how to incorporate it with the code to display all the field names and products with their primary IDs as in the code above. I have tried but failed miserably so far!

system
—
2009-12-22T17:43:41Z —
#16

better to go for separate file.later you can join files, but while learning it is much better to have separate files.

Also, it is much better for learning purpose to update and delete by one entry, not multiple.Or you'll stick for someone's help forever. You'll get it, yeah. But if you really want to learn, things must be learned distinctly. Step by step. From easy steps to more complicated. Multiple item forms are complicated.

juliamfg
—
2009-12-23T11:38:49Z —
#17

I agree learning in small steps is the best way but I seem to make big leaps forward in some directions and in others a tiny step seems to be a huge problem!As I said I used the code you suggested for the update, which works fine (the update I mean) but try as I might the best I can manage so far for adapting it for a delete is to get the Field names into a table but no field values and obviously no way of deleting what I cannot retrieve! It makes it very difficult not knowing the names of the fields and also not being able to use javascript for the confirmation of delete action. Can you help please?