0

I have a page where you can update a lot of data, I'm wondering what the best way to store this data on the backend is (using the least amount of code generally).

The data set on the page where each value can be changed would be something like this:

   ID NAME   DATA MURDATA FINALDATA
   1  MyName XYZ  425     Blah
   2  Anoth  ASDF 87      987
   3  Last   DKL  38      19bm

How do I loop through the post data so I can do updates on all this submitted data at once?

1 Answer 1

2

You can't update two tuples with different values. You will have to do one UPDATE for each tuple or do a bulk insert using the ON DUPLICATE KEY UPDATE-syntax. See the MySQL reference.

Have a form with inputs that have [] as suffix to their names. This way you can access these inputs as an array in PHP:

$result = mysql_query("SELECT * FROM table");
while ($tuple = mysql_fetch_assoc($result))
{
    echo '<input type="text" name="name['.(int)$tuple["id"].']" value="'.htmlentities($tuple["name"]).'" />';
    // echo more inputs ...
}

Your post processing code could look like this

foreach ($_POST["name"] as $id => $data)
{
    // todo: check if all $_POST columns are set, before accessing them
    // if (!isset($_POST["data"][$id], $_POST["murdata"][$id], ...)
    //     continue;

    $query = "UPDATE table SET ".
    $query .= "name = ".mysql_real_escape_string($_POST["name"][$id])."'";
    // the other columns...
    $query .= "WHERE id = '".(int)$id."'";
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm ok with doing an update for each. I'm just trying to figure out how to pull the data out for proper storage for each "row".
Oh my bad. I thought you were using a database like MySQL. If you just want to save the data and don't access it except for the update, you could just have a big text file and load it into a textarea. If you don't trust the user, who is doing the changes or you want to otherwise access the data, use a database like mysql.
i modified it further to make it more usable. hope you can make good use of the code. good luck.
Got it, thanks, I was missing that [] to get PHP POST variables which got my problem solved :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.