php删除空数据记录

正常发送表单时没有问题,但是当我用 jquery 单击添加电话按钮时,输入我有一个名为 sub_phone_ 的输入,当我发布它时,它保存在名为 sub_email、sub_map 和 sub_maps 的内容中我没有将其添加到 php 中的数据库中。我希望如果没有电子邮件地图地图输入,他们不应该将它们写入数据库。


数据库记录


[{"title":"1","address":"1","phone":"1","submenu":[{"email":"1","phone":null,"map":null,"maps":null}]}]

if empty

"phone":null,"map":null,"maps":null <<< delete.

超文本标记语言


<form action="" method="post" class="formify">

    <div class="tab-pane" id="iletisim">

        <div id="menus">

            <ul id="menu" class="menu">

                <li>

                    <div class="menu-item">

                        <a href="#" class="delete-menu">

                            <i class="fa fa-times"></i>

                        </a>

                        <div class="row">

                            <label class="col-sm-2 col-form-label">title</label>

                            <div class="col-sm-7">

                                <div class="form-group bmd-form-group">

                                    <input class="form-control" type="text" name="title[]" placeholder="title">

                                </div>

                        </div>

                    </div>

                    <div class="row">

                        <label class="col-sm-2 col-form-label">address</label>

                        <div class="col-sm-7">

                            <div class="form-group bmd-form-group">

                                <textarea class="form-control" name="address[]" cols="30" rows="3" placeholder="address""></textarea>

                        </div>

                    </div>

                </div>

                <div class="row">

                    <label class="col-sm-2 col-form-label">Phone</label>

                        <div class="col-sm-7">

                            <div class="form-group bmd-form-group">

                                <input class="form-control" type="text" name="phone[]" placeholder="Phone">

                            </div>

                        </div>

                </div>

            </div>


桃花长相依
浏览 98回答 1
1回答

MYYA

您可以尝试以下代码。&nbsp; foreach (post('title') as $key => $title) {&nbsp; &nbsp; &nbsp; &nbsp; $arr = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => $title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'address' => $address[$key],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'phone' => $phone[$key],&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; if (post('sub_email_' . $key) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (post('sub_email_' . $key)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $submenu = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $subphone = post('sub_phone_' . $key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $submap = post('sub_map_' . $key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $submaps = post('sub_maps_' . $key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (post('sub_email_' . $key) as $k => $subemail) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (($subphone[$k] != null) && ($submap[$k] != null) && ($submaps[$k] != null)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $submenu[] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email' => $subemail,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'phone' => $subphone[$k],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'map' => $submap[$k],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'maps' => $submaps[$k]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count($submenu) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arr['submenu'] = $submenu;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $menu[] = $arr;&nbsp; &nbsp; }&nbsp; &nbsp; $result=DB::table('menu')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->insert([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'menu_title' =>$menu_title,//your menu title here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'menu_content' => json_encode($menu)&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);我希望它对你有用。
打开App,查看更多内容
随时随地看视频慕课网APP