0

这是在数据库中添加数据的代码。但它是 else 部分。响应为空。我通过警报响应进行了检查。但它是空的。

    //add donation by ajaxs
function ccjkfoundation_adddonation()
{
    var dateobj  = jQuery('#donation_date');
    var nameobj  = jQuery('#donation_name');
    var desobj   = jQuery('#donation_des');
    var assetobj = jQuery('#donation_asset');
    var data = {
            action: 'ccjkfoundation_adddonation', 
            date:  dateobj.val(),
            name:  nameobj.val(),
            des:   desobj.val(),
            asset: assetobj.val()
        };
    jQuery.post(ajaxurl, data, function(response){
    alert(response);
        if(response)
        {
            var retstr = eval("("+response+")");
            jQuery('#donation-con').prepend(retstr['list']);
            jQuery('#other_donation_asset').text('¥'+retstr['ototalasset']).attr('title',retstr['ototalasset']);
            dateobj.val('');
            nameobj.val('');
            desobj.val('');
            assetobj.val('');
            ccjkfoundation_total_asset();
        }
        else
        {

            alert('Failure');
        }
    });
}

这是php函数。请检查这个,如果我需要更改数据库表可能是。

    function ccjkfoundation_adddonation_callback()
{
    ob_clean();
    $date  = trim($_POST['date']);
    $name  = trim($_POST['name']);
    $des   = trim($_POST['des']);
    $asset = trim($_POST['asset']);
    $ototalasset = get_option('ccjk_other_donation');
    if(empty($name) || empty($asset) || !is_numeric($asset))
        exit('');
    global $wpdb;
    $result = $wpdb->insert( DONATION_TABLE, array(
            'name' => stripslashes($name),
            'des' => stripslashes($des),
            'asset' => $asset,
            'date' => stripslashes($date)
            ));
    if(!$result)
        exit('');
    $donation_id = (int) $wpdb->insert_id;
    $ototalasset+=$asset;
    $return_json = array('ototalasset'=>number_format($ototalasset));
    update_option('ccjk_other_donation', $ototalasset);
    $liststr = '
        <tr id="donation_'.$donation_id.'">
            <td><input type="text" name="donation_date_'.$donation_id.'" id="donation_date_'.$donation_id.'"  class="form-required form-input-tip" placeholder="Date of Donation" aria-required="true" value="'.$date.'"></td>
            <td><input type="text" name="donation_name_'.$donation_id.'" id="donation_name_'.$donation_id.'"  class="form-required form-input-tip" placeholder="Donor Name" aria-required="true" value="'.$name.'"></td>
            <td><input type="text" name="donation_des_'.$donation_id.'" id="donation_des_'.$donation_id.'"   class="form-input-tip" placeholder="Donor Description" value="'.$des.'"></td>
            <td><input type="text" name="donation_asset_'.$donation_id.'" id="donation_asset_'.$donation_id.'" class="form-required form-input-tip" placeholder="0" aria-required="true" value="'.$asset.'"></td>
            <td><input type="button" class="button" value="Update" onclick="ccjkfoundation_updatedonation('.$donation_id.')"> <input type="button" class="button" value="Del" onclick="ccjkfoundation_deldonation('.$donation_id.');"></td>
        </tr>                       
        ';  
    $return_json['list'] = $liststr;
    echo json_encode($return_json);
    die(); // this is required to return a proper result 这里是必须的
}
4

1 回答 1

0

我相信您正在使用 WordPress 并且 $wpdb->insert() 有问题。“插入”方法中缺少“格式”参数。应该是这样的:

$result = $wpdb->insert( DONATION_TABLE,
                                array(
                                        'name' => stripslashes($name),
                                        'des' => stripslashes($des),
                                        'asset' => $asset,
                                        'date' => stripslashes($date)
                                ), 
                                array(
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s'
                                )
);

您应该能够根据需要调整“格式”标识符,例如字符串的 %s、整数的 %d 等。请参阅http://codex.wordpress.org/Class_Reference/wpdb#INSERT_rows

于 2013-09-20T06:05:43.830 回答