1

I wrote an user action analysis but I can't execute my insert into statement. I tried all suggestion on stack can't but figure out my mistake, I don't even get an error: it is running through without executing the statement. I have also an connection php - this works fine so the fault is not there. I would like to insert the action var later but at first I have to call the function. So whats wrong with my request or php function?

jQuery

$( document ).ready(function() {
  var action = '';
  document.addEventListener('click', function(e) {
    e = e || window.event;
    e = e.target || e.srcElement;
    if (e.nodeName === 'SECTION') {
      action = e.id;
      updateAction();
    }
  }, false);

  function updateAction() {
    $.ajax({
      type: "POST",
      url: '../actions/userAction.php',
      data: {action: 'test'},
      success: function(){
        console.log(action);
      }
    });
  }
});

PHP

<?php
class userAction
{
  /* User Action */
  public function updateAction(){
    if(isset($_POST['action']) && !empty($_POST['action'])) {
      $db = getDB();
      $st = $db->prepare("INSERT INTO user_analysis (action) VALUES ('bla') where userID = 1945");
      $st->execute();
      $db = null;
      $_SESSION['uid']=$uid;
      return true;
    }    else
    {
      return false;
    }
  }
}
?>
6
  • why i can't call my php function he is not inserting in db Commented Jun 16, 2017 at 7:00
  • First of all check whether you are entering updateAction() in this function? if entering then check you are getting value in post or not. And also query is wrong Commented Jun 16, 2017 at 7:14
  • INSERT command cannot have WHERE clause. IF you are updating a record, use UPDATE Commented Jun 16, 2017 at 7:15
  • Pankaj yeah this is the fault i think i can't enter the function and getting an syntax error in the comment here my whole code link Commented Jun 16, 2017 at 7:19
  • Where are you calling the PHP function updateAction()? Commented Jun 16, 2017 at 7:20

3 Answers 3

1

Where do you call updateAction() method? I assume you don't. Then why do you expect that it will execute? You have at least 3 ways to deal with it.


1. Make the method static and call it without instantiating a class

If you don't have any reason to construct an object of your class then you can call your method without doing it. Just add this line of code after your class definition:

userAction::updateAction()

This way you call the method which handles your POST request.

2. Instantiate your class and then call your method

The same story as above. The difference here is to construct an object first:

$ua = new userAction();
$ua->updateAction();

3. (the easiest) Get rid of the class and method

As the title suggest. Remove whole code of a class and leave only the body of your method. Like this:

if(isset($_POST['action']) && !empty($_POST['action'])) {
   $db = getDB();
   $st = $db->prepare("INSERT INTO user_analysis (action) VALUES ('bla') where userID = 1945");
   $st->execute();
   $db = null;
   $_SESSION['uid']=$uid;

   return true;
} else {
   return false;
}

This should be the whole content of your file: a condition.

BTW
1. Your query is not valid. If you want to UPDATE a row you need to use UPDATE statement, not INSERT
2. I would suggest you call your class UserAction (uppercase). Just to make it standardised and more intuitive
3. Why do you mix jQuery with VanillaJS? You use jQuery and bind an event using clear JS. Make it semantic and decide if you use jQuery or not.
jQuery way:

var action = '';
$(document).on('click', function(e) {
  e = e || window.event;
  e = e.target || e.srcElement;
  if (e.nodeName === 'SECTION') {
    action = e.id;
    updateAction();
  }
});



That's all. I hope I could help

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use UPDATE instead of INSERT. Try this below code:

UPDATE user_analysis SET action = 'bla' WHERE id = 1945

1 Comment

unfortunately no , problem is don't get any error so if i can debug it but i think it is an syntax error , the ajax call is right or ?
0
if(isset($_POST['action']) && !empty($_POST['action'])) {     
  echo $_POST['action'];
}

echo the value of $_POST['action'] inside your if loop...If not getting try to change the - type: "POST", to "GET",

2 Comments

try to solve the syntax error, and change the type as GET
I assume you forgot about ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.