首页 > 解决方案 > Ajax不发送数据php

问题描述

我有一个问题似乎已经持续了大约一天半;它让我彻夜难眠。我有一个如下所示的表格,我想单击一个按钮,然后相应地更新并将数据插入数据库。 在此处输入图像描述

上图为表格,点击支付后显示下图

在此处输入图像描述

为 Pinnacle 输入 8600 应该会使 pinnacle 的总汇款增加8,600,但之后没有任何反应。我成功了,但没有更新或插入数据库的表中。

在此处输入图像描述

上图显示values提交成功。创建了一个 ajax 来支持这一点,如下所示

<script>
     function payCompany(id, companyName, owed, oldRemit){
        swal("Enter Amount:", {
            title: companyName,
            buttons: true,
            closeModal: true,
            content: "input",
        })
        .then((amount) => {
            if (amount === "") {
                    swal("Oops!! You need to enter value.");
                return false
            }else{
                $.ajax({
                    type: 'POST', 
                    url: 'remit.php',
                    data:{
                    rid: id, 
                    pay: amount, 
                    company_Name: companyName,
                    old_Remit: oldRemit,
                    debt_owed: owed,
                      <?php 
                            echo  ' currentDate : "'.$savedate.'",'
                      ?>
                    },
                    success: function(data){
                         swal(`Paid: ${amount} to ${companyName}`);
                    },
                    error: function(data){
                        swal(`Error remitting: ${amount} to ${companyName}`);
                    }
                });
            } 
        });
     };


</script>

应该发送数据到

<?php
    
    #remit.php
    /*
        If you are using sessions you need to start a session!
    */
    error_reporting( E_ALL );

    session_start();

    if( empty( $_SESSION['useremail'] ) OR empty( $_SESSION['role'] ) OR $_SESSION['role']=="Admin" ){
        exit( header('Location: index.php') );
    }

    /*
        Check that all fields that are required in the sql have been submitted
    */
    if( isset( 
            $_POST['rid'],
            $_POST['pay'],
            $_POST['currentDate'],
            $_POST['compnyName'],
            $_POST['old_remit'],
            $_POST['debt_owed']
        ) ){

        try{
            
            include_once 'connectdb.php';

            /*
                When inserting, updating multiple tables there is some sense in using a transaction
                so that if one part fails the db is not littered with orphan records
            */
            $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $pdo->beginTransaction();

            $remitID            =   $_POST['rid'];
            $new_remit          =   (double)$_POST['pay'];
            $company            =   $_POST['compny_Name'];
            $old_rem            =   (double)$_POST['old_remit'];
            $total_debt         =   (double)$_POST['debt_owed'];
            $current_date       =   $_POST['currentDate'];

            $paid               =   $new_remit +  $old_rem;
            $currentDebt        =   $total_debt - $paid;

            /*
                The sql command should use placeholders rather than embedded variables - the names are arbitrary
            */
            $sql='INSERT INTO `tbl_category_remits` ( `payment_date`, `category`, `remitted` ) 
                VALUES 
            ( :pay_date, :comp, :remit )';
            $stmt=$pdo->prepare( $sql );
            $args=array(
                ':pay_date'        =>  $current_date,
                ':comp'            =>  $company,
                ':remit'           =>  $new_remit
            );
            if( !$stmt->execute( $args )  )echo 'stmt#1 failed';
            
            
            $sql='UPDATE `tbl_category` SET `remitted` =:payment , `debt` =:current_debt WHERE `catid`=:rid';
            $stmt=$pdo->prepare( $sql );
            $args=array(
                ':current_debt'     =>  $currentDebt,
                ':payment'          =>  $paid,
                ':rid'              =>  $remitID
            );
            if( !$stmt->execute( $args ) )echo 'stmt#2 failed';
            
            
            
            /*
                If it all went well, commit these statements to the db
            */
            if( !$pdo->commit() )echo 'commit failed';
            
            
        
        }catch( PDOException $e ){
            /*
                Any problems, rollback the transaction and report issues - 
                not necessarily with the full `getMessage()` ~ perhaps just
                'Error!' etc
            */
            $pdo->rollBack();
            echo $e->getMessage();
        }
    }
?>

我不确定为什么数据库似乎既没有更新也没有插入新数据。 在此处输入图像描述

在上述数据库中,没有记录更新。此外,也没有对此进行插入。 在此处输入图像描述

这是我的代码出现的主要延迟。我会很感激你的帮助。谢谢。

标签: phpsqlajax

解决方案


推荐阅读