PHP优化查询与foreach函数

时间:2015-01-16 16:02:14

标签: php mysql optimization foreach

我的this代码在这里有效,但显然没用。

我提出了this解决方案: 但我得到了Query empty


更新: 这是代码的工作优化 在评论中向Alex提供了帮助。希望我的解决方案能够帮助其他人。

$postedids = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 foreach ($postedids as $id){

 $columns = array('pickup', 'delivery');

    foreach ($columns as $c){
     $vid = mysql_real_escape_string(trim($_POST["$c"."$id"."id"]));
     $name = mysql_real_escape_string(trim($_POST["$c"."$id"."name"]));
     $address = mysql_real_escape_string(trim($_POST["$c"."$id"."address"]));
     $city = mysql_real_escape_string(trim($_POST["$c"."$id"."city"]));
     $state = mysql_real_escape_string(trim($_POST["$c"."$id"."state"]));
     $zip = mysql_real_escape_string(trim($_POST["$c"."$id"."zip"]));
     $directions = mysql_real_escape_string(trim($_POST["$c"."$id"."directions"]));
     $phone = mysql_real_escape_string(trim($_POST["$c"."$id"."phone"]));
     if($vid!=""){
     $consigneeupdate = "INSERT INTO `consignees` (`consigneeID`, `name`, `address`, `city`, `state`, `zip`, `directions`, `phone`) 
     VALUES ('$vid', '$name', '$address', '$city', '$state', '$zip', '$directions', '$phone')
        ON DUPLICATE KEY UPDATE 
    `name`= CASE WHEN `consigneeID`='$vid' THEN '$name' ELSE `name` END,
    `address`= CASE WHEN `consigneeID`='$vid' THEN '$address' ELSE `address` END,
    `city`= CASE WHEN `consigneeID`='$vid' THEN '$city' ELSE `city` END,
    `state`= CASE WHEN `consigneeID`='$vid' THEN '$state' ELSE `state` END,
    `zip`= CASE WHEN `consigneeID`='$vid' THEN '$zip' ELSE `zip` END,
    `directions`= CASE WHEN `consigneeID`='$vid' THEN '$directions' ELSE `directions` END,
    `phone`= CASE WHEN `consigneeID`='$vid' THEN '$phone' ELSE `phone` END ";
    }else{$consigneeupdate = "";}
    $consignees .= mysql_query($consigneeupdate) or (mysql_error());
    }

 }

1 个答案:

答案 0 :(得分:-2)

VALUES ('$id', '$name', '$address', '$city', '$state', '$zip', '$directions', '$phone')

应该是

VALUES ('".$id."', '".$name."', '".$address."', '".$city."', '".$state."', '".$zip."', '".$directions."', '".$phone."')