php/postgresql multiple update html page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhaynes
Forum Newbie
Posts: 18
Joined: Mon Nov 08, 2004 8:32 am

php/postgresql multiple update html page

Post by rhaynes »

Hi, I have a postgresql database for which I use php to do simple search tasks from drop down menus on a html page.

I want to create to a new series of pages which will allow me to do the following:

1) Search and display all entries in the database which meet a selected criteria
(I can pretty much take care of this since it is similar to what I have running)

2) The search could return 100's of results. I want to display these results with one or two fields displayed in text boxes (or similar) which would allow me to change the entries.

3) Upon changing some or all of the returned results I would like to submit the results and have all the changed entries (or all the entries would be o.k.) updated in the database.

Any pointers which would help with this task would be greatly appreciated.

R Haynes
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post by jl »

You're going to have a single form submitted that is updating multiple rows in a table, so you need to be able to determine which text boxes submitted apply to which rows in your data.

Possibly you're also going to page your output, so you'll only display n rows from some start ID to some end ID.

I'm not sure that this is the optimum way to do it, but my first thought on this is to generate something like the following when you output the text boxes for the form:

<input type="hidden" name="start_id" value="1000">
<input type="textbox" name="tb_1000" value="foo"> - or whatever kind of input you want
<input type="textbox" name="tb_1001" value="foo">
<input type="textbox" name="tb_1002" value="foo">
.... etc. up to name="tb_1009"
<input type="hidden" name="end_id" value="1009">

I.e. your giving the input fields for each row from your database a name that corresponds to the ID (PK) of the row from the db.

Then when that form is submitted, you do something like this with PHP:

Code: Select all

$start=$_REQUEST&#1111;'start_id'] ;
$end=$_REQUEST&#1111;'end_id'] ;

for ($k=$start ; $k <= $end ; $k++) &#123;

    $textbox_name='tb_'.$k ;

    $value=$_REQUEST&#1111;$textbox] ;

$sql='UPDATE table SET value='.$value.' WHERE ID='.$k ;

run $sql ;

&#125;
Which will go through all the text boxes submitted and UPDATE the database. Only small problem with this code is that it doesn't track which text boxes you changed and UPDATES all of them. You could fix that with some clever javascript perhaps.

(Obviously all form code and PHP to generate the initial form omitted as I'm assuming you'd know how to do that)
Post Reply