mysql—在添加具有相同属性的新记录后,从html中的表中删除一行

gev0vcfq  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(236)

我使用laravel在浏览器中显示一个表(来自mysql)。该表有3列(ip、时间、状态)。现在在mysql中添加一条新记录,表中添加新行时,我首先要检查新记录的ip是否已经在表中,它的时间和状态用新的值更新,换言之我要删除旧的行(如果ip相同的话)并向表中添加新的行,所以我总是有每个ip的最后时间和状态,而不是表中具有相同ip的重复行。这是我的密码:

<h1 Style = " margin-top : 100px;" >Last state of each Host</h1>
    <table  class="table">
        <thead>
        <tr>
            <th scope="col" >IP</th>
            <th scope="col">date_time</th>
            <th scope="col">status</th>

        </tr>
        </thead>
        <tbody>
        @if(isset($result))
            @foreach($result as $t)

                <tr class="bg-info">
                    @foreach($ipt as $s)
                        @if($t -> date_time === $s -> date_time)
                            <td>{!! $s -> IP!!}</td>
                        @endif
                    @endforeach
                    <td>{!! $t -> date_time!!}</td>
                    <td>{!! $t -> status!!}</td>

                </tr>
            @endforeach

        @endif

        </tbody>
    </table>

你知道我该怎么做吗?

nhaq1z21

nhaq1z211#

你可以使用一个有说服力的查询函数updateorcreate,通过使用这个函数你不必删除任何记录。看我的代码这绝对是帮你。

App\IPs::updateOrCreate([
'IP'   => $ip,
],[
'status' => $status,
'time' => $time,

]);

相关问题