我在这里遵循了一些线索,但我看不出我做错了什么:
$friends = array
(
array("yaron","levi","yaron@gmail.com"),
array("david","chohen","david@gmail.com"),
array("michal","rubin","michal@gmail.com"),
array("dafna","mishal","dafna@gmail.com"),
array("tamar","mishal","tamar@gmail.com")
);
if(is_array($friends)){
$sql = array();
foreach( $friends as $row ) {
$sql[] = '("'.mysql_real_escape_string($row['firstname']).'","'.mysql_real_escape_string($row['lastname']).'","'.mysql_real_escape_string($row['email']).'")';
}
mysql_query('INSERT INTO MyGuests (firstname,lastname,email) VALUES '.implode(',', $sql));
}
答案 0 :(得分:0)
您将收到undefined index
错误,因为您的数组没有firstname
,lastname
和{所以..在内爆期间用单引号括起值。这应该有帮助
if(is_array($friends)){
$sql = array();
foreach( $friends as $row ) {
$sql[] = mysql_real_escape_string($row[0]);
$sql[] = mysql_real_escape_string($row[1]);
$sql[] = mysql_real_escape_string($row[2]);
$values = "'".implode("','", $sql)."'";
mysql_query("INSERT INTO MyGuests (firstname,lastname,email) VALUES(".$values.")");
}
}
答案 1 :(得分:0)
将您的朋友数组更改为:
$friends = array(
array("firstname"=>"yaron", "lastname"=>"levi", "email"=>"yaron@gmail.com"),
array("firstname"=>"david", "lastname"=>"chohen","email"=>"david@gmail.com"),
array("firstname"=>"michal","lastname"=>"rubin", "email"=>"michal@gmail.com"),
array("firstname"=>"dafna", "lastname"=>"mishal","email"=>"dafna@gmail.com"),
array("firstname"=>"tamar", "lastname"=>"mishal","email"=>"tamar@gmail.com")
);
通过此更改,您的脚本将成功运行!