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:
- username = alpha , html = bla, bla class="bla"
- 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
.