请问,为什么无法把变量循环输入数据库中呢,是SQL语句错误还是程序嵌套问题?

[code="java"]<?php
$con = mysql_connect("localhost","root","5555555551");
//mysql_query("SET NAMES 'UTF-8'");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("zuobiao",$con); //选择数据库
for ($i=1;$i<=5;$i++){
$x= mysql_query("select x,y from t1 order by DrugSId asc limit 1,$i");
$rs = mysql_fetch_array($x);
$cc=$rs['y'].",".$rs['x'];
//echo $cc."
";
$url="https://maps.googleapis.com/maps/api/place/search/json?language=zh-CN&location=$cc&&radius=500&sensor=false&key=AIzaSyCxLAQfEbXSARI3UD7_Nkj0WbVvQqnQuX0";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$body=curl_exec($curl);
curl_close($curl);
$place=json_decode($body,true);
foreach ($place as $b){

           foreach((array)$b as $c){
          if(!is_array($c))
             continue;
               $d=$c['name'].",";
                //echo $d; 
                 $sql=mysql_query("insert into t1(keywords) values ('$d')");
                 // echo $sql
               // or  die ('SLQ Syntax error '.mysql_error());                     
                if ($d == null){
         continue;}
     }
 }  

}
?>

[/code]
数据库结构如下

[img]http://dl.iteye.com/upload/attachment/0073/0323/41ad7c14-c43d-3877-9bcc-6603a329135f.jpg[/img]

如果直接输出$d,是正常的,可以获得一串地名,但是如果插入数据库就只能获得一串地名中的一项.请问哪个地方写错了,如何修改?
为方便各位高手调试,给出1-5之内的数据

[img]http://dl.iteye.com/upload/attachment/0073/0340/f7b747fd-9bf0-3a2b-bc77-e3298e7f7a71.jpg[/img]

你是不是需要这样的功能;

1、首先根据x 和 y 到google 那边查 数据,如果查出来是多个区域[a,b,c],则更新到数据库时是a,b,c

2、因此你需要更新x 和 y的keywords功能 而不是插入,可以参考下面语句:
2.1、首先拿到所有keywords 通过“,”拼起来;
2.2、执行update

[code="java"]在 $cc=$rs['y'].",".$rs['x']; 定义

$xx = $rs['x'];
$yy = $rs['y'];

foreach ($place as $b){

$d = "";
foreach((array)$b as $c){

if(!is_array($c))

continue;

if($d != "") {
$d = ",".$d;
}
$d=$d.$c['name'];

}

$sql=mysql_query("update t1 set keywords = '$d' where x='$xx' and y='$yy'");

} [/code]

代码看不出来。

insert那句SQL打印出来看过吗?

话说,

看代码的意思是根据坐标x,y调用google map api查出地名关键字,然后更新回去。

如果是这样的话,不应该是insert吧?我怎么觉得应该是update呢?

[code="sql"]
update t1 set keywords = '$d' where x=XXX and y=YYY
[/code]

类似这样?

MySQL支持在一条INSERT语句中插入多条记录
建议你用
insert into t1(keywords) values ('上海'),('北京');
这种格式一次性插入