Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:19:43+00:00 2026-06-15T11:19:43+00:00

I am trying to update a row in a mysql database. To do this,

  • 0

I am trying to update a row in a mysql database. To do this, I would use a simple query like this:

    "UPDATE  `contactinfo` SET  `Company` =  'Google', WHERE `id` =  '1';"

The problem is that the database is dynamic and users can add columns at any time. So I do not know the names of the columns. To find the names and create a form to post to the page that will actually do the mysql work uses this code:

    <?php
            $result = mysql_query("select * from contactinfo WHERE `id` = '".$rid."';");

            if (!$result) {
                die('Query failed: ' . mysql_error());
            }
            $i = 0;
            while ($i < mysql_num_fields($result)) {
                $meta = mysql_fetch_field($result, $i);
                if (!$meta) {
                    echo "ERROR";
                }
                $name = $meta->name;

                $r = mysql_fetch_array(mysql_query("select * from contactinfo WHERE `id` = '".$rid."';"));
                $content = $r[$name];

                if($name != 'id') {
                    echo "<tr><td align='center'><div align='left'>Edit ".$name."</div></td></tr>";
                    echo "<tr><td align='center'><input type='text' value='" . $content . "' /></td></tr>";
                }
                $i++;
            }
            mysql_free_result($result);
        ?>

This creates a nice little table with input boxes that allow the user to edit the content of the row that has been selected. The row id number($rid) is used to identify which row needs to be changed.

My question is, how can I get the new content for the row from the posted form and create a query to update it? I can’t seem to figure out how to dynamically get the names of the form as well as the new content to the write the query.

If any clarification is needed just let me know and all help is appreciated.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:19:46+00:00Added an answer on June 15, 2026 at 11:19 am

    The easiest way is to name the fields in the form exactly how the name of the fields in the database are.
    Lets say you have this form

    <form action="">
       <input name="field[firstname]">
       <input name="field[lastname]">
       <input name="field[address]">
    </form>
    

    You should probably be able to create the form based on the fields names too, you are probably already doing this.
    In the file that processes the response you can do something like this:

    foreach($_POST['field'] as $field_name => $field_value) {
       $sql_str[] = "{$field_name} = '{$field_value}'";
    }
    

    This just goes through the ‘field’ array that comes from post and puts the proper update text into another array.
    Then just do a

    mysql_query("UPDATE contactinfo SET ".implode(',', $sql_str)." WHERE `id` = '".$rid."';")
    

    To put it into the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a row update from php to an mysql database. It
I'm trying to update a row in my database but it's not running. Below
I'm trying to run a UPDATE using REPLACE on my MySQL database. I have
I've been trying to update a specific row for a while now, and it
I'm trying to do a single row insert/update on a table but all the
I am trying update text views while this loop is processing. Here is my
I`m trying to update time in datetime field as UPDATE table_name SET col_name=to_DATE('04/02/2012 00:12:00','MM/DD/YYYY
I am trying to write a PHP-MySQL database processor that is somewhat intelligent. When
I'm trying to design a MySQL database to store user zipcode preferences for providing
I am trying to delete expired entries in a MySQL database, on creation or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.