Php 使用bind_param()函数添加多行

Php 使用bind_param()函数添加多行,php,mysql,database,mysqli,Php,Mysql,Database,Mysqli,所以我有这个代码 while($counter < $i){ $link = new mysqli('localhost', 'root', '', 'domaci2'); $query = 'INSERT INTO utisci VALUES(?,?,?,?,?,?)'; $statement = $link->prepare($query); $statement->bind_param("isssis", $id, $grad, $ime

所以我有这个代码

while($counter < $i){
$link = new mysqli('localhost', 'root', '', 'domaci2');

    $query = 'INSERT INTO utisci VALUES(?,?,?,?,?,?)';
    $statement = $link->prepare($query);    
    $statement->bind_param("isssis", $id, $grad, $ime, $opis, $ocena, $tagovie[$counter]);

    $statement->execute();

    mysqli_close($link);
    $counter++;
    }
while($counter<$i){
$link=newmysqli('localhost','root','domaci2');
$query='插入utisci值(?,,,,,,,,?)';
$statement=$link->prepare($query);
$statement->bind_param($id、$grad、$ime、$opis、$ocena、$tagovie[$counter]);
$statement->execute();
mysqli_close($link);
$counter++;
}
我想做的事情是在这个while循环中将多行添加到我的数据库中。正如您所看到的,所有内容都保持不变,唯一改变的是$tagovie[$counter]变量,它在每个下一个循环中都应该是不同的

但我只在数据库中保存了一行,第一行正好是。其他人只是被忽略了

我想知道是否有任何方法可以做到这一点或修改我的代码

while($counter < $i){
$link = new mysqli('localhost', 'root', '', 'domaci2');

    $query = 'INSERT INTO utisci VALUES(?,?,?,?,?,?)';
    $statement = $link->prepare($query);    
    $statement->bind_param("isssis", $id, $grad, $ime, $opis, $ocena, $tagovie[$counter]);

    $statement->execute();

    mysqli_close($link);
    $counter++;
    }
提前谢谢

我也尝试了这个代码,它是一样的

$link = new mysqli('localhost', 'root', '', 'domaci2');

    $query = 'INSERT INTO utisci VALUES(?,?,?,?,?,?)';
    $statement = $link->prepare($query);
    while($counter < $i){
       $statement->bind_param("isssis", $id, $grad, $ime, $opis, $ocena, $tagovie[$counter]);
       $counter++;
        }

    $statement->execute();

    mysqli_close($link);
$link=newmysqli('localhost','root','domaci2');
$query='插入utisci值(?,,,,,,,,?)';
$statement=$link->prepare($query);
而($counter<$i){
$statement->bind_param($id、$grad、$ime、$opis、$ocena、$tagovie[$counter]);
$counter++;
}
$statement->execute();
mysqli_close($link);

您应该在循环外部打开和关闭数据库连接。这同样适用于prepare语句,您只需要准备一次

您的问题可能是由绑定变量的方式引起的:

 $statement->bind_param("isssis", $id, $grad, $ime, $opis, $ocena, $tagovie[$counter]);
                                  ^^^ this would be the problem
我假设第一列ID有一个唯一的约束,所以在第一次插入之后,所有后续的插入都会失败,因为ID在循环中没有改变


一遍又一遍地添加相同的行似乎也没有什么意义,只是有一些小的变化,你应该好好看看你的表结构和数据库规范化。

为什么要在循环中关闭mysql连接?但我会在下一个循环中打开它?@user3616184 Put
错误报告(E\u ALL)
ini\u集('display\u errors',1)
在PHP文件的开始处打印错误。您只需连接到数据库一次,因此在循环的顶部编写连接,并在其中定义这些变量
$id、$grad、$ime、$opis、$ocena、
,这与我打开数据库的位置相同实际上我会在每个循环中创建新的$id生成代码。现在它不是主键,它只是一个没有意义的数字。我想创建一个一对多相似的表,这就是为什么我有一些变化。@user3616184您应该添加错误处理。将此添加到脚本顶部,并发布您在问题中遇到的任何异常:
mysqli_报告(mysqli_报告错误| mysqli_报告严格)@user3616184顺便问一下,您的表没有主键吗?不知怎的,$id是主键。报告功能帮助很大。谢谢你真的解决了我的问题