[英]Unable to insert more than one row in MySQL?
Here is my PHP code: 这是我的PHP代码:
$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代码:
$.ajax({
type: "POST",
url: 'savedarticle.php',
data: {
html: innerHTML,
username: '<?php echo $_SESSION['user']; ?>'
}
});
$html
has a unique value every time. $html
每次都有一个唯一值。 Let's say I have two values: 假设我有两个值:
By class I want to signify there are double quotes in the HTML but I am escaping them. 按类,我想表示HTML中有双引号,但我正在转义它们。 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.
如果我先单击1,它将被保存,然后单击2将没有任何作用。 If I click on 2 first it will be saved and later clicking on 1 won't have any effect.
如果我先单击2,它将被保存,然后单击1不会有任何效果。 In case it matters I have another table with same column name
username
. 万一重要,我还有另一个具有相同列名
username
表。 Also my table savedarticles
has an Id
column with int
values, username
column with type varchar(255)
and article
column with type text
. 另外,我的表
savedarticles
具有一个具有int
值的Id
列,具有varchar(255)
类型的username
列和具有text
类型的article
列。
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."')");
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.