无法在WordPress上编辑functions.php文件

时间:2018-12-04 09:07:05

标签: php wordpress

我正在将以下代码添加到WordPress中的functions.php文件中,并得到syntax error, unexpected ' ' (T_STRING)错误。

add_action('wp_ajax_nopriv_save_order_data', 'save_order_data');
add_action('wp_ajax_save_order_data', 'save_order_data');
function save_order_data() {
     global $wpdb;
     $total_amt = $_POST['amount'];
     $stmt = $wpdb->query("INSERT INTO order_master (payment_amt) VALUES ($total_amt)"  );
     $result = $wpdb->query($wpdb->prepare($stmt));

     if($result)
     {
         $res="Data Inserted Successfully:";
         echo json_encode($res);
     }
     else {
         $error="Not Inserted,Some Probelm occur.";
         echo json_encode($error);
     }
}

在此行出现错误:

$stmt = $wpdb->query("INSERT INTO order_master (payment_amt) VALUES ($total_amt)"  );

1 个答案:

答案 0 :(得分:0)

您的代码有误:

$stmt = $wpdb->query("INSERT INTO order_master (payment_amt) VALUES ($total_amt)"  );
$result = $wpdb->query($wpdb->prepare($stmt));

应为:

$stmt = "INSERT INTO order_master (payment_amt) VALUES (%d)";
$wpdb->query( $stmt,$total_amt);

完整代码:

add_action('wp_ajax_nopriv_save_order_data', 'save_order_data');
add_action('wp_ajax_save_order_data', 'save_order_data');
function save_order_data() {
     global $wpdb;
     $total_amt = $_POST['amount'];
     $stmt = "INSERT INTO order_master (payment_amt) VALUES (%d)";
     $result=$wpdb->query( $stmt,$total_amt);

     if($result)
     {
         $res="Data Inserted Successfully:";
         echo json_encode($res);
     }
     else {
         $error="Not Inserted,Some Probelm occur.";
         echo json_encode($error);
     }
}

希望这对您有用。