>我有一个这样的多维数组。
[rows] => Array
(
[0] => Array
(
[0] => fname1
[1] => flabel1
[2] => numeric
[3] => yes,no
[4] => h1
[5] => Y
[6] => a1
[7] => <input id="1" value="remove" type="button">
)
[1] => Array
(
[0] => fname2
[1] => flabel2
[2] => text
[3] => yes,no
[4] => h2
[5] => Y
[6] => a2
[7] => <input id="2" value="remove" type="button">
)
[2] => Array
(
[0] => fname3
[1] => flabel3
[2] => text
[3] => yes,no
[4] => h3
[5] => N
[6] => a3
[7] => <input id="3" value="remove" type="button">
)
)
)
我想在每个数组中删除。而且我无法将这些值插入MySql
这是我的查询。
$sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ( ) ";
我试过这个
$rows = $_REQUEST['rows'];
foreach($rows AS $row)
{
print_r($row);
}
这给了我数组值。 但是如何插入MySql。
尝试使用这个:
$rows = $_REQUEST['rows'];
foreach($rows AS $row)
{
array_pop($row);//remove last element
print_r($row);
//since you have fixed index then your sql will become.
$sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]') ";
}
unset ($array_name[count($array_name)-1]);
$rows = $_REQUEST['rows'];
foreach($rows AS $row)
{
unset ($row[count($row)-1]);
}
您可以在foreach中删除数组中的最后一个元素,然后将数组内爆为字符串并使用sql
foreach ($rows as $row)
{
unset($row[count($row)-1]);
$mysqlStr = implode(',',$row);
$sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ({$mysqlStr}) ";
}
$sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ";
$values = array();
foreach($rows as $row){
unset($row[count($row)-1]);
$values[] = implode('", "', $row);
}
$sql .= '( "'.implode('", "', $values).'")';
这会将数据存储在单个查询中。你使用mysqli_real_escape_string来验证值。