0

I have this backend php file and in this file there is this code :

  if (isset($_POST['send_comment'])) {

    $post_id = $_POST['post_id'];
    $community_id = $_POST['community_id'];
    $comment_content = $_POST['comment_content'];
    date_default_timezone_set('UTC');
   $date =   date('Y-m-d H:i:s T', time());

    $sql = "INSERT INTO comments (user_id, community_id, post_id, comment, date_time) VALUES (?, ?, ?, ?, ?)";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
      echo "There was a problem, please try again";
    } else {
      mysqli_stmt_bind_param($stmt, "iiiss", $_SESSION['user-id'], $community_id, $post_id, $comment_content, $date);
      mysqli_stmt_execute($stmt);
      $result = mysqli_stmt_get_result($stmt);
    }

    echo $result;



  }

I have a .js function that console.log(this.responsetext). But in the console it displays nothing. There is a line, but it echoed "". There are no errors, and the sql statement doesn't work, nothing is added to the database. I don't know if this will help but here is the js code that calls this :

function sendcomment(post_id, community_id) {

  comment_content = document.getElementById(post_id).getElementsByClassName('comment-send')[0];

  if (comment_content.value == "") {
    alert("You have to to have something in your comment to send it!");
  } else if (comment_content.textLength > 2000) {
    alert("Sorry but your comment is too long!");
  } else {

    var fd = new FormData();
    fd.append("post_id", post_id);
    fd.append("community_id", community_id);
    fd.append("comment_content", comment_content.value);
    fd.append("send_comment", true)
    var xhr = new XMLHttpRequest();
    var fullurl = '../backend/communitybackend.php';
    xhr.open('POST', fullurl, true);
    xhr.onload = function() {
      if (this.status == 200) {
        console.log(this.responseText);
        commentsAppear(post_id, community_id);

      }
    }
    xhr.send(fd);

  }
}

Thanks!

TheDoc
  • 151
  • 6
  • 2
    According to the [docs](https://www.php.net/manual/en/mysqli-stmt.get-result.php), the result is `false`(???) for successful insert queries. Echoing `false` produces no output, i.e. `""`. Have you turned on all error reporting? Try `echo mysqli_error();` –  Apr 29 '21 at 10:45
  • It doesn't echo anything, and the php script isn't able to insert values – TheDoc Apr 29 '21 at 10:45
  • 1
    What settings do you have for error reporting (especially for mysqli)? – Nigel Ren Apr 29 '21 at 10:51
  • Don't rely on `echo` to try and debug your application. First step: check PHP's error log. Is anything being reported there? If not, then it's time to dump some values. Make sure your program enters the `if (isset($_POST['send_comment']))` branch. If it does, then you need to debug [mysqli specifically](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param/22662582#22662582). – El_Vanja Apr 29 '21 at 10:52

0 Answers0