dpp66953 2010-10-05 17:20
浏览 31
已采纳

只需单击一个提交按钮,即可将多行插入DB

I've got a little problem i hope you're able to help me fix.

First of all my DB table that is involved look like this:

+---------------+--------------+------+-----+-------------------+----------------+
| Field         | Type         | Null | Key | Default           | Extra          |
+---------------+--------------+------+-----+-------------------+----------------+
| id            | int(11)      | NO   | PRI | NULL              | auto_increment |
| KlubID        | varchar(11)  | NO   |     | 0                 |                |
| Medlemsnummer | varchar(11)  | NO   |     | 0                 |                |
| KlubType      | varchar(128) | NO   |     |                   |                |
| Handling      | varchar(128) | NO   |     |                   |                |
| Tidspunkt     | timestamp    | NO   |     | CURRENT_TIMESTAMP |                |
+---------------+--------------+------+-----+-------------------+----------------+

Say i got X entries where "Handling" equals Y in my table lets call the results P . Now I would like to take all P and insert a row foreach P where the value of "Handling" now will be equal to Q.

The problem here is that I want to do the insert with one button (submit) not multiple forms.

In advance thank you very much for your help. Feel free to ask for more info if this needs more clarity.

/Nicki

EDIT: For more clarity

public static function find_todays_children() {
    global $db;
    $sql = "SELECT * 
            FROM (
                SELECT * 
                FROM Handlinger 
                AS a
                WHERE date(Tidspunkt) = curdate() 
                AND Tidspunkt = (
                    SELECT max(Tidspunkt) 
                    FROM Handlinger 
                    AS b
                    WHERE a.Medlemsnummer = b.Medlemsnummer
                    )
                )
            AS c
            ORDER BY handling DESC, medlemsnummer";
    return static::find_by_sql($sql);
}

This query above returns the following.

+------+--------+---------------+----------+----------+---------------------+
| id   | KlubID | Medlemsnummer | KlubType | Handling | Tidspunkt           |
+------+--------+---------------+----------+----------+---------------------+
| 5786 | 0      | 1             | FK       | Kommet   | 2010-10-06 13:48:06 |
| 5787 | 0      | 2             | FK       | Kommet   | 2010-10-06 13:48:10 |
| 5789 | 0      | 4             | FK       | Kommet   | 2010-10-06 13:48:16 |
| 5790 | 0      | 3             | FK       | G?et     | 2010-10-06 13:48:27 |
+------+--------+---------------+----------+----------+---------------------+

I then want to be able to insert 3 rows where the field "Handling" is another value. I can do this by HTML form with PHP but I can't figure out how to do it all by only clickin once... The important thing is that not to insert anything where the latest entry for a specific "Medlemsnummer" (user_id) is already equal to the value "Gået"

My form looks like this:

...
    if(isset($action->Handling) != "Kommet") {
        $do_action = "Kommet";
    } else {
        $do_action = "Gået";
    }
...

<section>
    <form action="phineaslog_barn.php" method="post">
        <label for="Status">Klub Navn: </label>
        <input type="text" name="Medlemsnummer" value="<?php echo $child->Medlemsnummer; ?>" />
        <input type="text" name="Handling" value="<?php echo $do_action; ?>" />
        <input type="submit" name="submit" value="<?php echo $do_action; ?>" />
    </form>
</section>
  • 写回答

3条回答 默认 最新

  • dongyou7292 2010-10-06 12:21
    关注

    My understanding of the question:

    • For each row which is the final entry for a user, where handling = kommet

      • Insert another row with handling = gået, tidspunkt = now()

    So... to combine the best of two worlds...

    INSERT INTO tablename (KlubID,Medlemsnummer,KlubType,Handling,Tidspunkt)
        SELECT KlubID,Medlemsnummer,KlubType,'Gået',NOW()
        FROM Handlinger 
        AS a
        WHERE date(Tidspunkt) = curdate() 
        AND Handling = 'Kommet'
        AND Tidspunkt = (
            SELECT max(Tidspunkt) 
            FROM Handlinger 
            AS b
            WHERE a.Medlemsnummer = b.Medlemsnummer
         )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置