php-foreach()中的bind_param无法正确传递 [英] php - bind_param inside foreach() not correctly passing
问题描述
我确定这是重复的,但是我在这里的站点上尝试了几种不同的方法,但没有一种对我有用.我在php中调用函数,发送$ mysqli连接,$ clientID和要上传的$ tagFields数组.
I'm sure this is a duplicate, but I've tried several different things on the site here and none of them are working for me. I'm calling my function in php, sending $mysqli connection, the $clientID, and the array of $tagFields to upload.
正在运行,但值始终为null.我已将echo"$ tagName"放在foreach中,它正在读取它,但没有将其发送到数据库.但是,$ clientID是传递信息.因此,基本上,所有要做的就是将一致的空白行上载到我的数据库中.我在这里做什么错了?
It's 'working', but the values are always null. I've put echo "$tagName" inside the foreach, and it's reading it, but not sending it up to the database. $clientID, however, IS passing information. So basically all it does is upload consistently blank rows into my database. What am I doing wrong here?
function tagRefresh($mysqli,$clientID,$tagFields) {
$stmt = $mysqli->stmt_init();
$query = "INSERT INTO client_tags (client_id,tag_category) VALUES (?,?) ";
$stmt->prepare($query);
foreach($tagFields as $tagName) {
$stmt->bind_param('is',$clientID,$tagName);
$stmt->execute();
}
}
$ tagFields的一些示例值:
some sample values for $tagFields:
$tagFields[0] = "Regional";$tagFields[1] = "Automotive";$tagFields[2] = "Maintenance";
推荐答案
注意 mysqli_stmt :: bind_param 绑定变量的引用.请尝试以下操作:
Note mysqli_stmt::bind_param bind the reference of the variables. Try the below:
function tagRefresh($mysqli,$clientID,$tagFields) {
$stmt = $mysqli->stmt_init();
$query = "INSERT INTO client_tags (client_id,tag_category) VALUES (?,?) ";
$stmt->prepare($query);
$stmt->bind_param('is', $clientID, $tagName);
foreach($tagFields as $tagName) {
$stmt->execute();
}
}
这篇关于php-foreach()中的bind_param无法正确传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!