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>';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向