[英]Insert multiple rows in to table using php
Am trying to insert multiple rows in to table using php: 我正在尝试使用php在表中插入多行 :
<?php
$host = "localhost";
$username = "mysql_username";
$password = "mysql_password";
$dbname = "employee";
$con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));
$st = mysqli_prepare($con, 'INSERT INTO emp(name, gender, designation) VALUES (?, ?, ?)');
// bind variables to insert query params
mysqli_stmt_bind_param($st, 'sss', $name, $gender, $designation);
for ($x = 0; $x <= 3; $x++) {
$name = 'tom';
$gender = 'male';
$designation = 'developer';
mysqli_execute($st);
}
//close connection
mysqli_close($con);
?>
But the rows that i want to insert are not saved in database. 但是我要插入的行未保存在数据库中。 Are there any mistakes in my code ? 我的代码中有任何错误吗?
Actually I want the for loop from json array , I just test using for loop for knowing it is worked or not. 其实我想要json数组的for循环 ,我只是测试使用for循环来知道它是否有效。
I think the code is right, but try this: 我认为代码是正确的,但是请尝试以下操作:
<?php
$host = "localhost";
$username = "mysql_username";
$password = "mysql_password";
$dbname = "employee";
$con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));
$st = mysqli_prepare($con, 'INSERT INTO emp(name, gender, designation) VALUES (?, ?, ?)');
for ($x = 0; $x <= 3; $x++) {
$name = 'tom';
$gender = 'male';
$designation = 'developer';
// bind variables to insert query params
mysqli_stmt_bind_param($st, 'sss', $name, $gender, $designation);
mysqli_execute($st);
}
//close connection
mysqli_close($con);
?>
The mysqli_stmt_bind_param($query, 'is',…)
means the first value is an integer (i) and the next value is a string (s). mysqli_stmt_bind_param($query, 'is',…)
表示第一个值是整数(i),下一个值是字符串(s)。 Feel free to adjust to best fit your actual data types. 随时进行调整以最适合您的实际数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.