更新多个字段

时间:2016-08-04 09:03:46

标签: php mysql

我在管理面板上有多个字段用户可以添加字段并删除字段,同时添加它我使用foreach循环放置了简单的插入查询但是如果用户删除字段或更新则很难理解更新字段的概念如果我删除1个字段并更新它,它将删除2个或更多然后2个字段,当我尝试更新它没有更新好更新问题是因为我会在查询中犯一些错误。但主要的是关于逻辑,我无法正确构建它需要帮助。

更新查询

$video_link = $_POST['video_link'];
$old_links  = count($video_link);
if(isset($_POST['video_id'])) {
    $video_id   = $_POST['video_id'];
    $total_id   = count($video_id); 
} else {
    $video_id   = '';
}
$video_links = mysqli_query($connect, "SELECT * FROM video_slides WHERE model_id = '$model_id'");
$total_links = mysqli_num_rows($video_links);

$video_link = sizeof($video_link) - 1;
if($total_links >= 1) {
    for($i = 0; $i<=$video_link; $i++) {
        if(empty($video_id[$i])) {
            mysqli_query($connect, "INSERT INTO `video_slides`(`embeded_link``, `model_id`) VALUES ('$video_link[$i]', '$model_id')");
        }
        $query2 = mysqli_query($connect, "UPDATE `video_slides` SET `embeded_link`='$video_link[$i]' WHERE id='$video_id[$i]'");
        if($video_link < $total_links) {
            $new_total = $total_links-sizeof($video_link);
            for($j = 0; $j<=$new_total; $j++) {
                mysqli_query($connect, "DELETE FROM video_slides WHERE id='$video_id[$j]'");
            }
        }
    }
        } else {
            for($i = 0; $i<=$video_link; $i++) {
                if(empty($video_id[$i])) {
                    mysqli_query($connect, "INSERT INTO `video_slides`(`embeded_link``, `model_id`) VALUES ('$video_link[$i]', '$model_id')");
                }
            }
        }

这是我的表单字段

<div class="form-group">
    <label>Video Slides <input type="button" class="add_field_button btn blue" value="Add Field" /></label>
    <div class="input_fields_wrap">
    <?php 
        $sql3 = mysqli_query($connection, "SELECT * FROM video_slides WHERE model_id = '".$data['id']."'");
        if(mysqli_num_rows($sql3) == 0) {
    ?>
    <div class="new">
        <input type="text" id="video_link" size="20" name="video_link[]" placeholder="Embeded Video Link" class="form-control" />
    </div>
    <?php 
        } else {
            while($video = mysqli_fetch_assoc($sql3)) {
    ?>
    <div class="new">
        <input type="text" id="video_link" size="20" name="video_link[]" placeholder="Embeded Video Link" class="form-control" value="<?php echo $video['embeded_link']; ?>" />
        <input type="hidden" value="<?php echo $video['id']; ?>" name="video_id[]" />
        <a class="remove_field"><i class="fa fa-times"></i></a>
    </div>
    <?php } } ?>    
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

你应该尝试在一个单独的类中外包你的数据库连接 - 这将导致更好的可读代码。像Doctrine这样的ORM绝对可以帮助您更好地理解您自己的代码。

答案 1 :(得分:1)

根据我的理解,你只需要这些东西来制作复杂的编码

$video_link = $_POST['video_link'];

//First Remove All ID
mysqli_query($connect, "DELETE FROM video_slides WHERE model_id='$model_id'");

//Then After insert updated data
foreach($video_link as $key=>$val){
    mysqli_query($connect, "INSERT INTO `video_slides`(`embeded_link`, `model_id`) VALUES ('$val', '$model_id')");
}