0

Here is my PHP code:

$html = $_POST['html'];
$username   = $_POST['username'];
$connection = new mysqli('host', 'username', 'password', 'database');
$username   = $connection->real_escape_string($username);
$html       = $connection->real_escape_string($html);
$connection->query("INSERT INTO savedarticles (username, article) VALUES ('$username', '$html')");

Here is my AJAX code:

$.ajax({
  type: "POST",
  url: 'savedarticle.php',
  data: {
    html: innerHTML,
    username: '<?php echo $_SESSION['user']; ?>'    
  }
}); 

$html has a unique value every time. Let's say I have two values:

  1. username = alpha , html = bla, bla class="bla"
  2. username = alpha , html = more bla class="bla"

By class I want to signify there are double quotes in the HTML but I am escaping them. If I click on any one of these two the value are stored in database but no subsequent values are stored. If I click on 1 first it will be saved and later clicking on 2 won't have any effect. If I click on 2 first it will be saved and later clicking on 1 won't have any effect. In case it matters I have another table with same column name username. Also my table savedarticles has an Id column with int values, username column with type varchar(255) and article column with type text.

ydoow
  • 2,969
  • 4
  • 24
  • 40
SanJeet Singh
  • 1,291
  • 2
  • 15
  • 28
  • Please do not build queries by interpolating strings (`'$username'`), this is incredibly unsafe and brittle. Prepared statements were made to address this issue: http://stackoverflow.com/a/1290995/252218 – BoppreH Oct 29 '15 at 02:17
  • Does `$html` contains html elements? – A J Oct 29 '15 at 02:20
  • try to print log on ajax to see if it's still working after first trigger. – ydoow Oct 29 '15 at 03:40
  • What triggers `$.ajax({`? Are there any errors in the console? – chris85 Oct 29 '15 at 03:55

1 Answers1

0

I have edited your code. Please have a look and try .

<?php
$html = $_POST['html'];
$username   = $_POST['username'];
$connection = mysqli_connect('host', 'username', 'password', 'database');
$username   = $connection->real_escape_string($username);
$html       = $connection->real_escape_string($html);
$connection->query("INSERT INTO savedarticles (username, article) VALUES ('".$username."', '".$html."')");

?>
Shaymol Bapary
  • 468
  • 3
  • 11