Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code works. The comment works fine, but the result always goes to error on AJAX and displays 200 OK in the console window. Why is it that it jumps to an error instead of a success?

Another problem is that, despite using the addslashes function, the quotes are not handled and mysql error occurs.

What I have tried:

<?php
// php file for call $GLOBALS.
?>
<script type="text/javascript">

$(document).ready(function(){
   $(document.on('click', '.upload_comment', function(){
       let id = $(this).attr("id");
       let content = $("#comment-post-"+id).val();
       let text = $("#comment-textvalue-"+id).val();
       let customer = <?php echo $GLOBALS['user']; ?>;

       let object = {
          content: id,
          user: customer,
          comment: text
       }

       let jsonData = JSON.stringify(object);

       $.ajax({
          type: "POST",
          url:  "upload-comment.php",
          data: {data: jsonData},
          success: function( html ){
             $("ul#comment-"+id).append(html);
             $("#comment-textvalue-"+id).val("");
          },
          error: function( xhr ){  
             console.log(xhr.status+' '+xhr.statusText);
          }
        });
        return false;

   });
});
</script>

//upload-comment.php
if( isset( $_POST['data'] ) ){
  $data = json_decode( $_POST['data'] );

  foreach( $data as $value){
      $value = htmlentities( $value, ENT_QUOTES, "UTF-8" );
      $value = addslashes( $value );  
  }

  $conn = db_connect();
  $result = $conn->query{"select * from wc_comments") or die("access wc_comments failed: " . mysqli_error( $conn ) );
  if( $result ){
      $result = $conn->query("insert into wc_comments(content, author, date, comment") VALUES('".$data->{'content'}."', '".$data->{'user'}."', now(), '".$data->{'comment'}.'")")or die("insert failed: " . mysqli_error($conn));
     echo "<li class='comment'>".$comment."</li>";
  }
}
Posted
Updated 25-Apr-22 22:41pm
Comments
Richard Deeming 26-Apr-22 3:59am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
Chopin2001 27-Apr-22 5:34am    
Thanks for your comment.

1 solution

Quote:
Another problem is that, despite using the addslashes function, the quotes are not handled and mysql error occurs.

I suspect you should correct quotes errors:
PHP
$result = $conn->query("insert into wc_comments(content, author, date, comment") VALUES('".$data->{'content'}."', '".$data->{'user'}."', now(), '".$data->{'comment'}.'")")or die("insert failed: " . mysqli_error($conn));

comment") VALUES should be comment) VALUES
.'")") should be ."')")


Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v2
Comments
Chopin2001 27-Apr-22 5:34am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900