tags:

views:

370

answers:

3

Update mysql with $_POST data from while loop

I'm trying to get this round my head, but i am failing quite hard :[

I have 3 rows in a database which i am echoing out in a while() loop. A user can change search_terms and then save the fields in mysql, however i don't quite know how to do this, as there are 3 rows, so i can't just do;

<?php
    if($_POST['update']) {
        mysql_query("....");
    }
?>

Here is my code;

<?php
    $box_query = mysql_query("SELECT * FROM `ta_boxs`");
    while($box = mysql_fetch_array($box_query)) {

        echo '<div class="box_content">';
        echo '<div class="box">'."\n";
        echo '<span class="box_top"></span>';
        echo '<div class="box_header"><input type="text" id="title" name="title" class="title-input" value="'.$box['title'].'" /></div>';
        echo '<span class="sept"></span>';
        echo '<div class="admin-back">';
        echo '<form id="form-'.$box['id'].'" name="form-'.$box['id'].'" method="post" action="">';
        echo '<p class="sub"><span>Includes:</span> Search for these terms in Twitter Feeds.</p>';
        echo '<p class="sub-small">Please enter one word per field or click "add word".</p>';
        echo '<p><input type="text" id="search_term_1" name="search_term_1" value="'.$box['search_term_1'].'" class="term-input" />';
        echo '<p><input type="text" id="search_term_2" name="search_term_2" value="'.$box['search_term_2'].'" class="term-input" />';
        echo '<p><input type="text" id="search_term_3" name="search_term_3" value="'.$box['search_term_3'].'" class="term-input" />';
        echo '<span class="hr"></span>';
        echo '<p class="sub"><span>Excludes:</span> Ignore these terms in Twitter Feeds.</p>';
        echo '<p class="sub-small">Please enter one word per field or click "add word".</p>';
        echo '<p><input type="text" id="search_term_1" name="search_term_1" value="'.$box['exc_search_term_1'].'" class="term-input" />';
        echo '<p><input type="text" id="search_term_2" name="search_term_2" value="'.$box['exc_search_term_2'].'" class="term-input" />';
        echo '<p><input type="text" id="search_term_3" name="search_term_3" value="'.$box['exc_search_term_3'].'" class="term-input" />';
        echo '<input type="hidden" id="update" name="update" value="yes" />'
        echo '<p><input type="submit" id="update_'.$box['id'].'" name="update_'.$box['id'].'" value="Update" /></p>';
        echo '</form>';
        echo '</div>';
        echo '</div>'."\n";
        echo '<span class="box_bottom"></span>';
        echo '</div>';  
    }
?>

There could be 1 output, or 100, but i need a way to save them all, onsubmit. Any ideas?

A: 

make one form ,

and in this form set all fields,

one button to submit ,

give to the input elements unique id to every row like you do to the form with the box id

Haim Evgi
+2  A: 

Since you're outputting each box in a different <form> you'll only every get one back. If you change:

<input type="hidden" id="update" name="update" value="yes" />

to

<input type="hidden" id="update" name="update" value="' . $box['id'] . '" />

Then you'll be able to look which one was posted back.

Greg
So simple. Why do i always look for the hardest solution!?Thanks alot
paulOr
+1  A: 

I don't think your approach will work because you are generating multiple forms, so PHP will only get the one form that is submitted, and you won't be able to save all the changes on your page.

You might want to consider using one form and naming your inputs like an array (see http://php.net/manual/en/faq.html.php#faq.html.arrays) .

e.g. (simplified)

<form method="post">

<?php while($box = mysql_fetch_array($box_query)): ?>

<!-- div box thing -->  

<input name="box[<?php echo $box['id'];?>][search_term_1]" value="<?php echo $box['search_term_1']; ?>">
<input name="box[<?php echo $box['id'];?>][search_term_2]" value="<?php echo $box['search_term_2']; ?>">
<input name="box[<?php echo $box['id'];?>][search_term_3]" value="<?php echo $box['search_term_3']; ?>">

<input name="box[<?php echo $box['id'];?>][exc_search_term_1]" value="<?php echo $box['exc_search_term_1']; ?>">
<input name="box[<?php echo $box['id'];?>][exc_search_term_2]" value="<?php echo $box['exc_search_term_2']; ?>">
<input name="box[<?php echo $box['id'];?>][exc_search_term_3]" value="<?php echo $box['exc_search_term_3']; ?>">

<!-- end div box thing -->

<?php endwhile; ?>

</form>

If you print_r($_POST) after submitting that form you should see that it will be fairly easy to loop over/process.

Tom Haigh
nice and clean! +1
Shadi Almosri