www.webdeveloper.com
Results 1 to 7 of 7

Thread: user update forms for database....tricky question

  1. #1
    Join Date
    Sep 2005
    Location
    Leicestershire, UK.
    Posts
    64

    Question user update forms for database....tricky question

    Hi,

    I run a database to which users have write access to only certain fields.

    These fields appear in a an html table when they login to my website along with a whole load of other fields to which they don't have write access.

    Now. when they add something to one field, they click an update button and it updates that field and refreshes the whole page.

    I have just added three more fields that they can update. with similar update buttons in each one.
    but this is a bit of a pain. because it is a very long winded operation to update 400 rows of information when there are 4 fields in each row. that's 1200 updates and 1200 browser refreshes.

    so....is it possible to have just ONE update button per row...that will update all four fields in that row.

    Or one for the whole of the table?

    I'd prefer just one for each row at the moment because I've a feeling that putting one button to update the whole table could be more complicated but then what do I know! haha.
    An extract from the script is below....
    the rest of the script is just checking the user, querying the database etc...and so is left out for brevity.

    I'd be bloody grateful if someone could help...

    Thanks guys.

    .....

    <tr>

    <td></font><div align="center"><font face="tahoma"><font color="#FF9933" size="3"><strong><?php echo

    "$row->sample_id"?></strong></font></a> </font></div></td>

    <td><div align="center"><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" value="<?php

    echo "$row->farmer" ?>" name="farmer" maxlength="100"> <input type="hidden" value="<?php echo "$row->sample_id" ?>"

    name="sample_id" maxlength="40"> <input type="hidden" value="<?php echo "$row->user_id" ?>" name="user_id" maxlength="40">

    <input type="submit" name="add_farmer" value="Update">
    </form></div></td>

    <td><div align="center"><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" value="<?php

    echo "$row->location" ?>" name="location" maxlength="100"> <input type="hidden" value="<?php echo "$row->sample_id" ?>"

    name="sample_id" maxlength="40"> <input type="hidden" value="<?php echo "$row->user_id" ?>" name="user_id" maxlength="40">

    <input type="submit" name="add_location" value="Update">
    </form></div></td>

    <td><div align="center"><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" value="<?php

    echo "$row->account" ?>" name="account" maxlength="100"> <input type="hidden" value="<?php echo "$row->sample_id" ?>"

    name="sample_id" maxlength="40"> <input type="hidden" value="<?php echo "$row->user_id" ?>" name="user_id" maxlength="40">

    <input type="submit" name="add_account" value="Update">
    </form></div></td>





    <td><div align="center"> <?php echo "$row->analysis_date" ?></div></td>
    <td><div align="center"><?php echo "$row->commodity" ?></div></td>
    <td><div align="center"><?php echo "$row->moisture" ?></div></td>
    <td><div align="center"><?php echo "$row->pro_db" ?></div></td>
    <td><div align="center"><?php echo "$row->sp_weight" ?></div></td>
    <td><div align="center"><?php echo "$row->nitrogen_db" ?></div></td>
    <td><div align="center"><?php echo "$row->oil_content_asis" ?></div></td>
    <td><div align="center"><?php echo "$row->hagberg" ?></div></td>
    <td><div align="center"><?php echo "$row->twofive" ?></div></td>
    <td> <?php echo "$row->twotwofive" ?></td>
    <?php
    if ($row->commodity == 'Oats' | $row->commodity == 'Rye' | $row->commodity == 'Wheat' | $row->commodity == 'Triticale') { ?>
    <td> <div align="center"><?php echo "$row->not_retained" ?></div></td>
    <?php
    } else { ?>
    <td>NA</td>
    <?php
    }
    ?>
    <td><div align="center"><?php echo "$row->admixture" ?></div></td>
    </tr>

    <?php } ?>
    </tbody>
    </table>



    So you see the script for the three update buttons....well basically - can we make it one?

    Thanks

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    of course you can, just wrap the whole row in a single form, instead of breaking out each field into its own form.

    then when you process the page, make sure each form element has a different name, parse it out of the $_POST array, and update all 3 columns

    UPDATE `your_table` SET col1='val1', col2='val2', col3='val3' WHERE...
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  3. #3
    Join Date
    Sep 2005
    Location
    Leicestershire, UK.
    Posts
    64
    Thanks, that bit is easy enough now you mention it, but I'm having some trouble with the update command in the SQL.

    here's what I've got....


    $update = "UPDATE results SET farmer = '".$_POST['farmer']."' WHERE sample_id = '".$_POST['sample_id']."' AND user_id =

    '".$_POST['user_id']."'";

    which updates the field "farmer" based on a certain user and his access to certain sample IDs. but the update command needs to also update the other fields "location" and "account" so how to I add location and account to the string above....??

    Thanks.

    Andrew

  4. #4
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Quote Originally Posted by Andrewpca
    Thanks, that bit is easy enough now you mention it, but I'm having some trouble with the update command in the SQL.

    here's what I've got....


    $update = "UPDATE results SET farmer = '".$_POST['farmer']."' WHERE sample_id = '".$_POST['sample_id']."' AND user_id =

    '".$_POST['user_id']."'";

    which updates the field "farmer" based on a certain user and his access to certain sample IDs. but the update command needs to also update the other fields "location" and "account" so how to I add location and account to the string above....??

    Thanks.

    Andrew
    As I posted already....

    UPDATE `your_table` SET col1='val1', col2='val2', col3='val3' WHERE...

    Are you not sure how your code should look?
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  5. #5
    Join Date
    Sep 2005
    Location
    Leicestershire, UK.
    Posts
    64
    yeh i totally missed that bit of your message. i've done it now. Thanks for your help. I appreciate it. I've got to add another field as a drop down list (contents in another table) this field will also be user updated and will be updated by the same button. don't know whether you can help with this? I'm not sure how to do drop downs...

    Thanks a lot though.

    Andrew

  6. #6
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    I still don't understand why people directly inject their POST values into the sql string...most webhosts have magic quotes off.

    Always filter out the user input by applying mysql_escape_string().

    "Everything in a web browser."

  7. #7
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Quote Originally Posted by Andrewpca
    yeh i totally missed that bit of your message. i've done it now. Thanks for your help. I appreciate it. I've got to add another field as a drop down list (contents in another table) this field will also be user updated and will be updated by the same button. don't know whether you can help with this? I'm not sure how to do drop downs...

    Thanks a lot though.

    Andrew
    Drop downs are exactly the same, if not easier.

    if your select/option look like

    <select name="bob">
    <option value="sam">My name</option>
    </select>

    Then $_POST['bob'] would contain "sam".

    Also, Bueza is right. you should look into actually parsing. You might want to start over in the PHP forum...
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles