Enter several variables in the table, assigning each corresponding record

I am showing the data in an html table, with a drop down list with a list of places. Each volunteer will be assigned a place. I guess I can go down through the html table and designate each volunteer as a meeting place. The drop-down field contains all the possible places to which they can be assigned.

<select>
<option value="1">Setup</option>
<option value="2">Check in</option>
etc...
</select>

Then, as soon as I finish assigning each volunteer, I want to click submit, and he will assign an appropriate value for each volunteer.

How would I do this, I know how to do it, but only one at a time.

+3
source share
4 answers

Change the name of each choice so that it includes the volunteer identifier:

<select name="venues[1]">
<option value="1">Setup</option>
etc...
</select>

<select name="venues[2]">
<option value="1">Setup</option>
etc...
</select>

<select name="venues[3]">
<option value="1">Setup</option>
etc...
</select>

$_POST : 1, 2, 3 ( ) , .

$_POST['venues'] :

foreach ($_POST['venues'] as $volunteer_id => $venue) {
  save_venue_for_volunteer($volunteer_id, $venue);
}
+3

, . : MySQL ( MySQL) , (InnoDB does, MyISAM ).

<?php

if ( isset( $_POST['venuChoice'] ) )
{
    //  Create a transaction
    mysql_query( 'BEGIN' );

    $failure = false;

    //  Loop over the selections
    foreach ( $_POST['venuChoice'] as $employeeId => $venueId )
    {
        $sql = sprintf(
                'UPDATE table SET columns=%d WHERE id=%d'
            ,   intval( mysql_real_escape_string( $venueId ) )
            ,   intval( mysql_real_escape_string( $employeeId ) )
        );
        if ( ! @mysql_query( $sql ) )
        {
            $failure = true;
            break;
        }
    }

    //  Close out the transaction
    if ( $failure )
    {
        mysql_query( 'ROLLBACK' );
        //  Display and error or something
    } else {
        mysql_query( 'COMMIT' );
    //  Success!
    }
}

?>

<form>
    <select name="venueChoice[1]">
        <option value="1">Setup</option>
        <option value="2">Check in</option>
    </select>
    <select name="venueChoice[2]">
        <option value="1">Setup</option>
        <option value="2">Check in</option>
    </select>
    <select name="venueChoice[3]">
        <option value="1">Setup</option>
        <option value="2">Check in</option>
    </select>

</form>

, , POST, UPDATE , .

+1

. SQL 1 MySQL:

UPDATE volunteer SET venue = 1 WHERE id = 2;
UPDATE volunteer SET venue = 2 WHERE id = 3;
...

I would like to have a way, but I think that in each request there will be too many different options to make it one.

0
source
foreach ($_POST['venues'] as $volunteer_id => $venue) {
    save_venue_for_volunteer($volunteer_id, $venue);
}

function save_venue_for_volunteer($volunteer_id, $venue) {
    $result = mysql_query("UPDATE volunteers_2009 SET venue_id='$venue' WHERE id='$volunteer_id'") 
    or die(mysql_error());
}

The table in which it will be saved will be volunteers_2009, so how should it be?

0
source

Source: https://habr.com/ru/post/1698050/


All Articles