简体   繁体   English

无法在MySQL中插入多行?

[英]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: 假设我有两个值:

  1. username = alpha , html = bla, bla class="bla" 用户名= alpha,html = bla,bla class =“ bla”
  2. username = alpha , html = more bla class="bla" 用户名= alpha,html =更多bla class =“ bla”

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM