0

I am trying to pass some values to a mysql table but I do something wrong. I call this function from somewhere:

function updatebets(postid, bet, betamount){

alert(postid + " " + bet + " " + betamount);


 $.ajax({
      type: 'POST',
      url: 'current.php',
      data: {bankpostid: postid, bankbet: bet, bankbetamount: betamount},
      success: function(){
          alert('works');
      },
      error: function(){
         alert('something went wrong');
      }
 });

}

This is the current.php page:

<?php
global $current_user, $wpdb;
$uid = $current_user->ID;


$postid = $_POST['bankpostid'];
$bet = $_POST['bankbet'];
$betamount = $_POST['bankbetamount'];


$sql = "INSERT INTO bets (postid, uid, bet, betamount) VALUES ('$postid', '$uid', '$bet' , '$betamount')";
$wpdb->query($sql);

?>

The fields "postid, uid, bet, betamount" are the name fields of the table I want to update. I get the error 'something went wrong'. I am working with wordpress, the page current.php is in the theme folder.

Student
  • 47
  • 1
  • 7

1 Answers1

0

I know, you need update data in your database but you run SQL INSERT DATA.

If you want Update data in your database you must use SQL UPDATE.

Here is the example

$sql = "UPDATE bets SET postid='$postid', uid='$uid', bet='$bet' , betamount='$betamount' WHERE betsprimary='$betsprimary'

The betsprimary='$betsprimary' syntax you can replace with primary key coloumn and the value in your table.

Rohman HM
  • 2,539
  • 3
  • 25
  • 40
  • Actually the problem is that the current.php page doesn't get called. Is there any mistake in the ajax syntax? – Student Sep 28 '16 at 03:55
  • Can you attach your folder structure? So I can see how to write in your code – Rohman HM Sep 28 '16 at 03:57
  • I don't know how to do that. I am working with wordpress. The page current.php is inside the theme folder. – Student Sep 28 '16 at 04:00
  • If you check your console is show the error? file not found or anything? If I didn't know your folder structure I can't help you write right path your current.php file – Rohman HM Sep 28 '16 at 04:03
  • When you run your codes what you get? 'works' or 'something went wrong' – Rohman HM Sep 28 '16 at 04:05
  • something went wrong, so I guess the url path must be false if the ajax code is ok – Student Sep 28 '16 at 04:08
  • It's in another .php file. I believe the problem is that I don't import the current.php file in my code. It's location is in wp-content/themes/mytheme/current.php. Any idea how and where to import it in my code? – Student Sep 28 '16 at 04:17
  • I included it using include (TEMPLATEPATH . '/current.php'); but I still getting "something went wrong" error. – Student Sep 28 '16 at 04:27
  • try remove the backslash (TEMPLATEPATH . 'current.php') – Rohman HM Sep 28 '16 at 04:35
  • How can I access network console? I removed "/" but I have the same error. – Student Sep 28 '16 at 05:21
  • What browser are you using it? If you using Chrome, just click Network tab. and you can see your request to your server. – Rohman HM Sep 28 '16 at 09:15
  • Ok so I changed the path of current.php file to absolute path. Now when I run the code,in the browsers console I get the status error [HTTP/1.0 500 Internal Server Error 16ms]. Any idea how can I fix this? It's about permissions? – Student Sep 28 '16 at 13:52
  • maybe this link can help you http://stackoverflow.com/questions/4789613/ajax-500-internal-server-error – Rohman HM Sep 28 '16 at 13:58