dousha1394
2019-03-22 19:21
浏览 68
已采纳

如何使用单个提交按钮插入和更新

I have this issue with code below.What am i trying to do is to with single submit button INSERT data in database and when its inserted if i need to change something i need to be able to perform a UPDATE on current day only to database on inserted row.But so far i am able to INSERT data but when i try to change something its insert just another row in db.

if(isset($_POST['submit'])) { 

    $TO_P20O = filter_input(INPUT_POST, 'TO_P20O', FILTER_SANITIZE_STRING);
    $TO_P21O = filter_input(INPUT_POST, 'TO_P21O', FILTER_SANITIZE_STRING);


        $to_ochtend = $conn->prepare(" SELECT * FROM tablePL WHERE created >= '".$huidige_dag."' order by id ");
        $to_ochtend->execute();
        $to_ochtend = $to_ochtend->fetch(PDO::FETCH_ASSOC);


        $query = "INSERT INTO tablePL (id, TO_P20O, TO_P21O, created ) VALUES (NULL, ?, ?, NOW())";
        $stmt = $conn->prepare($query);
        $stmt->bindParam(1, $TO_P20O, PDO::PARAM_STR);
        $stmt->bindParam(2, $TO_P21O, PDO::PARAM_STR);
        $stmt->execute();
        echo "<script type='text/javascript'>
        alert('Data inserted successfully.');
        window.location.replace(\"$domain/succes\");
        </script>";

}

else {  

    $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);

    $update_query = "UPDATE tablePL SET TO_P20O = ?, TO_P21O = ?, created = NOW() WHERE id = ? AND created >= ? LIMIT 1"; 
    $stmt = $conn->prepare($update_query);
    $stmt->bindParam(1, $TO_P20O, PDO::PARAM_STR);
    $stmt->bindParam(2, $TO_P21O, PDO::PARAM_STR);
    $stmt->bindParam(3, $id, PDO::PARAM_INT);
    $stmt->bindParam(4, $huidige_dag, PDO::PARAM_STR);
    $stmt->execute();

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchunxian9740 2019-03-22 19:50
    已采纳

    To accomplish this in my code I simply add a hidden input in the form to pass a $action variable to the parse page.

    echo '<form action="parse-content-types.php" method="post">';
        echo '<input type="hidden" id="returnto" name="returnto" value="manage-content-types.php">';
        echo '<input type="hidden" id="action" name="action" value="add">';
          include('form-content-types.php');
    
        echo '<div class="row">';
            echo '<div class="col_12 center alpha omega">';
                echo '<input type="submit" class="button blue" value="Submit this Content Type">';
            echo '</div>';
        echo '</div>';
    
    echo '</form>';
    

    This would be the parse-content-types.php:

    <?php
        $level = '../../';
        include($level.'core/ini/start.php');
    
        $returnto = $_POST['returnto'];
        $action = $_POST['action'];
    
        $content_type_id = $_POST['content_type_id'];
        $content_type = $_POST['content_type'];
        $content_type_path = $_POST['content_type_path'];
        $content_type_desc = $_POST['content_type_desc'];
    
        if($action == 'add'){
            $insertq = $db->prepare('INSERT INTO content_types (
                content_type,
                content_type_path,
                content_type_desc
            )
            VALUES (?,?,?)');
            $insertq->execute(array(
                $content_type,
                $content_type_path,
                $content_type_desc
            ));
    
            $msg = 's|The item: '.$content_type.' has been inserted into the Content Types Database Table.';
        }
        elseif($action == 'edit'){
            $sql = 'update content_types set
                content_type_id=:content_type_id,
                content_type=:content_type,
                content_type_path=:content_type_path,
                content_type_desc=:content_type_desc
            where content_type_id=:THEID';
    
            $upd=$db->prepare($sql);
            $upd->bindParam(':content_type_id',$content_type_id,PDO::PARAM_STR);
            $upd->bindParam(':content_type',$content_type,PDO::PARAM_STR);
            $upd->bindParam(':content_type_path',$content_type_path,PDO::PARAM_STR);
            $upd->bindParam(':content_type_desc',$content_type_desc,PDO::PARAM_STR);
    
            $upd->bindParam(':THEID',$content_type_id,PDO::PARAM_STR);
            $upd->execute();
    
            $msg = 's|The item: '.$content_type.' has been updated in the Content Types Database Table.';
        }
    
        echo '<Script language="javascript">window.location="'.$returnto.'?msg='.$msg.'"</script>';
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题