php save data into two rows at a time in database?

白昼怎懂夜的黑 提交于 2019-12-25 08:43:17

问题


I have a form like this

<form class="product-data" action="">
    <table>
        <tr class="data-row">
            <td> 
                <input type="number" name="finance[a][source_unit]" >
            </td>
            <td >
                <input type="number" name="finance[a][target_unit]">
            </td>
            <td>
                <input type="number" name="finance[a][client_price]">
            </td>
            <td>
                <input type="number" name="finance[a][client_salary]" >
            </td>
    </tr>
    <tr class="data-row">
        <td>
            <input type="number" name="finance[b][source_unit]" >
        </td>
        <td >
            <input type="number" name="finance[b][target_unit]">
        </td>
        <td>
            <input type="number" name="finance[b][client_price]">
        </td>
        <td>
            <input type="number" name="finance[b][client_salary]" >
        </td>
    </tr>
    </table>
</form>

here you can see I have two rows. One for a and another for b. Now I want to save them in the database with two rows. One for the a and another for b at a same time. When I am doing print_r(finance). with this code

$finances = $_POST['finance'];
    print_r($finances);

Its showing me an array like this

Array
(
    [a] => Array
        (
            [source_unit] => 3213
            [target_unit] => 657654322343
            [client_price] => 5435.00
            [client_salary] => 897.00
        )

    [a] => Array
        (
            [source_units] => 67656565
            [target_units] => 43243
            [client_price] => 23432.00
            [client_salary] => 6546.00
        )

)

Now can someone tell me how to save them in each row. I have my database table is like this and the data should be saved like this

Id, product_type, source_unit, target_unit, client_price, lient_salary
1       A            3213        657654322343     5435           897
2       B            67656565     43243           23432           6546

回答1:


I have two solutions for you. Once that is tailored for this scenario only:

$f = $_POST['finance'];

// insert first row
$query = "INSERT INTO `table` VALUES (NULL, 'A', {$f['a']['source_unit']}, {$f['a']['target_units']}, {$f['a']['client_price']}, {$f['a']['client_salary']})";
mysql_query($query);

// insert second row
$query = "INSERT INTO `table` VALUES (NULL, 'B', {$f['b']['source_unit']}, {$f['b']['target_units']}, {$f['b']['client_price']}, {$f['b']['client_salary']})";
mysql_query($query);

or if you have it more universal (for multiple rows):

$f = $_POST['finance'];

foreach($f as $key => $item) {
    // assign key of the letter as value to insert
    $letter = strtoupper($key);

    // insert a row
    $query = "INSERT INTO `table` VALUES (NULL, '{$letter}', {$item['source_unit']}, {$item['target_units']}, {$item['client_price']}, {$item['client_salary']})";
    mysql_query($query);
}



回答2:


loop thru your array and either insert or update accordingly.

    foreach($finances as $key => $data)
    {
    //based on the $key if value exists in database update else insert
    echo $key.'<br />';
    echo $data['source_unit'].'<br />';
    echo $data['target_unit'].'<br />';
    echo '<hr />';
    }


来源:https://stackoverflow.com/questions/28498185/php-save-data-into-two-rows-at-a-time-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!