1

I am adding below code to functions.php file in WordPress and getting syntax error, unexpected ' ' (T_STRING) error.

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);
     }
}

Getting error on this line:

$stmt = $wpdb->query("INSERT INTO order_master (payment_amt) VALUES ($total_amt)"  );
  • 2
    Are you sure that the code is the one running while the error is thrown? According to https://3v4l.org/r9RXB, there is no syntax error in that snippet – Nico Haase Dec 04 '18 at 09:26

1 Answers1

-1

Your code have some mistake :

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

It should be like :

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

Full Code :

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);
     }
}

Hope this will work for you.

Tristup
  • 3,603
  • 1
  • 14
  • 26
  • Can you explain how the original code triggers a syntax error? – Nico Haase Dec 04 '18 at 09:24
  • @NicoHaase, see we are here assuming that the issue specified is due to the code given. Once Jhon confirm that issue resolve I will explain you the issue. – Tristup Dec 04 '18 at 09:27
  • Thanks @Tristup it worked for me –  Dec 04 '18 at 09:33
  • @NicoHaase, As I told you. This is the error : $result = $wpdb->query($wpdb->prepare($stmt)); Where it expect a string instead of result object. Hope it is clear to you now. Please go through the https://developer.wordpress.org/reference/classes/wpdb/prepare/ – Tristup Dec 04 '18 at 09:42
  • 1
    @Tristup please add such explanation to the answer itself, such that others can learn from it. Please don't put such relevant information into the comment section – Nico Haase Dec 04 '18 at 10:32
  • Additionally, I still don't get how this change affects a syntax error. Of course, it would throw an error on execution, but not the "unexpected string" error – Nico Haase Dec 04 '18 at 10:34
  • @NicoHaase, it was obivious to add a explanation in answer. I focused on answer and later I will provide the explanation if it was asked by John. – Tristup Dec 04 '18 at 11:09
  • @NicoHaase, A better explanation for you. Prepare statement always expect a string to be a first parameter. Where as this was a result object, which has it own structure.If you print the $stmt, you can easily understand the issue. Please experiment little before asking this. – Tristup Dec 04 '18 at 11:11
  • An explanation should always be given, such that others can learn from your answer. SO is not only about fixing individual problems, but about providing long-lasting help. If someone else searches for a solution for the problem about that syntax error and finds this answer, he should be able to apply it to its own code. And, as always, provide such explanation in the answer, not in the comment section – Nico Haase Dec 04 '18 at 11:12
  • @NicoHaase, You are quite correct. But if anyone knows the prepare function, he/she can easily understand it. Which I checked with prepare statement function. I already given you the explanation, and without going through it you are more concentrating on diifferent things. Please read the explanation I believe you will understand, if you still not able to, please contact me. Hope you dont mind to stop our conversation here. – Tristup Dec 04 '18 at 11:17
  • Sorry to jump in but Nico is right, an Answer is much more useful if all the explanations are given inside the answer itself. If it was a low rep user, I would copy paste all relevant comments inside the answer and mark the comments as "no longer needed" for a moderator to delete them. Given that you already have SO experience I won't bother doing that, but beware that's how this site operates. – brasofilo Dec 06 '18 at 12:51