Php 重复字段数组的mysql数组插入
我有这个PHP代码要插入mysqlPhp 重复字段数组的mysql数组插入,php,mysql,arrays,Php,Mysql,Arrays,我有这个PHP代码要插入mysql require_once("../form/corefile/functions.php"); $fn = new Functions(); $offers = $_REQUEST; $offer=$_REQUEST['offer']; $website =$_REQUEST['website']; $keyword =$_REQUEST['keyword']; $count_offer = count($_REQUEST['offer']); var_d
require_once("../form/corefile/functions.php");
$fn = new Functions();
$offers = $_REQUEST;
$offer=$_REQUEST['offer'];
$website =$_REQUEST['website'];
$keyword =$_REQUEST['keyword'];
$count_offer = count($_REQUEST['offer']);
var_dump($offer);exit;
for($i=0;$i<$count_offer ;$i++){
$_offer = ($offer[$i]);
$_website = ($website[$i]);
$_keyword = ($offer[$i]);
$query = $fn->InsertQuery("INSERT INTO offers (offer, website, keyword) VALUES ('$_offer','$_website','$_keyword')");
//var_dump($query);exit;
if($query)
{
$msg="Data Successfully Saved";
}
}
我得到一个错误:
注意:第19行repeat.php中的数组到字符串转换
其中第19行是$query=$fn->insertQuery
看起来我在从数组中提取正确的数据时犯了一些错误。数组$offer是一个关联数组的数组。$offer[0]将返回一个单数键值对数组,该数组当然不是字符串,因此出现错误 $\u offer
是一个数组。你可能需要的是$\u提供['name']
@PaulSpiegel-你很合适。但是,您能告诉我应该如何使用like$\u offer['name']
构建sql查询吗。在执行此操作时,我仍然会遇到错误-数组到字符串的转换
{$\u offer['name']}
应该可以工作。或者您分配$\u offer=$offer[$i]['name']
。但最好的方法是使用带有参数绑定的准备好的语句。您的代码对sql注入开放。不要使用addslashes()
<代码>mysqli\u real\u escape\u string()
是您应该执行的最低要求。你可能还是忘了清理数字。使用事先准备好的陈述,你就不用担心了。
array (size=2)
0 =>
array (size=1)
'name' => string 'offer1' (length=6)
1 =>
array (size=1)
'name' => string 'offer2' (length=6)