如何从RETS搜索查询中插入表中的数据?

时间:2014-12-27 06:22:52

标签: php mysql wordpress rets

我正在尝试在表格中插入数据。我正在使用搜索查询,然后在条件中使用FetchRow来获取行中的数据并希望直接在表中插入此行。我在this link中给出了表格的结构。

while条件中表中插入值的代码如下:

 require_once (ABSPATH . 'wp-admin/includes/upgrade.php');
 $query = "(922=MIAMI), (131=2014-12-16+)";
    $search = $rets->SearchQuery("Property", $class, $query, array("SystemName" => 1, 'Limit' => 1 ));

           if ($rets->NumRows($search) > 0) 
            {
                        $fields_order = $rets->SearchGetFields($search);
                        $this_record = array();
                        while ($record = $rets->FetchRow($search)) 
                        {

                        foreach ($fields_order as $fo)
                        { 
                        echo $this_record[] = $record[$fo];
                        }

                         $comma_fields_order = implode(",", $fields_order);
                         $comma_record = implode(",", $this_record );

                        echo $dfsdf = "INSERT INTO rets_property_2 (".$comma_fields_order.") VALUES (" .$comma_record.")";

                        dbDelta($dfsdf); 

                        }

           }

如果我回显插入查询,则为:

INSERT INTO rets_property_2 (sysid,1,10,11,13,14,17,19,21,22,25,28,29,30,31,39,45,47,53,54,56,57,59,61,62,63,66,69,71,73,74,75,76,80,92,93,97,98,99,100,102,106,109,110,111,113,114,115,125,126,127,128,129,131,134,136,137,143,144,149,150,157,158,160,161,164,165,167,178,180,181,188,190,193,194,195,206,207,214,223,225,226,227,229,230,232,242,245,246,247,248,250,252,261,263,264,266,267,268,274,275,283,294,295,296,314,315,319,320,321,322,324,326,327,328,329,330,331,332,333,334,335,336,337,338,341,342,347,348,351,352,353,354,355,356,360,361,362,363,364,366,367,368,369,370,372,373,374,375,376,881,886,891,893,894,922,924,1088,1218,1223,1329,1333,1335,1337,1339,1358,1410,1462,1463,1464,1465,1473,1485,1486,1487,1488,1490) VALUES (305578689,Condo/Co-Op/Villa/Townhouse,33131,,,,3003,42,None,,4,,,,Entry Level,Miami Board of Realtors,0,,Cbs Construction,,,Electric Cooling,,Miami-Dade County,,,,ASIA CONDOMINIUM,,,,,,2008-06-12T14:19:47,5,2630,,,,,2,,1,Electric Heat,Condo,9,Yes,ASIA IS BRICKELL KEY ISLAND'S PREMIER LUXURY BLDG. W/ ONLY 123 UNITS, PRIVATE ELEVATORS, SPECTACULAR VIEWS AND MUCH MORE.,,,,,3813,2014-12-23T09:16:55,EWM 09,0648464,6000000,,Daysi Morey,,1,M1242705,,No,,Vacant,EWM Realty International,305-329-7600,,Maximum 20 Lbs,Yes,,0043,,Funding,Yes,5000000,T,EXOTIC LUXURY AND STRIKING DESIGN MAKES ASIA THE # 1 RESIDENCE FOR THE MOST DISCRIMINATING OWNER. SMART TECHNOLOGY, PRIVATE ELEVATORS, FLOOR TO CEILING WINDOWS, BREATHTAKING VIEWS OF BISCAYNE BAY, BAYSIDE, MIAMI RIVER AND MIAMI;S EXHUBERTANT SKYLINE. 12' CIELINGS,MARBLE BATHROOMS, GOURMET CHEF STYLE KITCHEN, LUXE BATH WITH FRENCH VANILLA IMPORTED MARBLE AND MUCH MORE MAKES THIS REMARKABLE RESIDENCE A SANCTUARY NESTLED IN BISCAYNE BAY. ALLOW 24 HOURS NOTICE FOR SHOWINGS.,,,6,6,,,0,,2014-01-24T10:02:22,Active-Available,BRICKELL KEY BLVD.,900,,,4663,Tax Reflects City & County Tax,New Construction,,,42,,0,2007,,None,Yes,Bay Front,2008,Under Construction,Bike/Jog Path,Elevator,0,No Approvals,,,ASIA CONDOMINIUM,,No,,,,Dishwasher,Dryer,Microwave,Electric Range,Refrigerator,Other Equipment/Appliances,Open Balcony,,30,Wood Floors,,Condominium,Elevator,Foyer Entry,Other Interior Features,0,All Amenities,Building Exterior,Cable Tv,1920,365,,,1,1 Assigned Space,2 Or More Spaces,,,0,Other Restrictions,,Elevator Secure,Garage Secured,Lobby Secured,,Condo 5+ Stories,34,Other,Condo,Corner Unit,High Rise,123,,Bay,,900 BRICKELL KEY BLVD. # 3003,,,,0,Miami,Florida,,,http://instatour.propertypanorama.com/instaview/mia/M1242705,2009-02-19T16:00:26,,,,ASIA CONDO UNIT 3003,1573.564,No HOPA,,,2389,No,No,No,No,900 BRICKELL KEY BLVD. 3003,Yes,)

但表格中没有插入任何内容。哪里错了?有没有其他方法可以将fetchrow直接给出的整行插入表中?

请告诉我正确的方向。我认为问题在于数据类型,数字和varchar。那么如何格式化数据来自数组的插入查询?

我知道我的插入查询是错误的。

如果我尝试这个

INSERT INTO  `rets_property_2` (  `sysid` ,  `1` ) 
VALUES ( 526252,  'dsfsdfsdf' )

这样可以正常工作。但问题是如果值是字符串,那么如何从数组中获取值呢?somestringvalue'如果值是整数,那么只有整数值,然后将它们包装在一个数组中?

2 个答案:

答案 0 :(得分:1)

$query = "(922=MIAMI), (131=2014-12-16+)";
    $search = $rets->SearchQuery("Property", $class, $query, array("SystemName" => 1, 'Limit' => 5 ));
     if ($rets->NumRows($search) > 0) {


                $fields_order = $rets->SearchGetFields($search);

                while ($record = $rets->FetchRow($search)) {

                    //var_dump($fields_order);
                        foreach ($fields_order as $fo) { 


                           if( is_numeric($record[$fo])){
                               $valuesd[] = $record[$fo];
                           }else{

                              $onlyconsonants = str_replace("'", "", $record[$fo]);
                               $valuesd[] =  "'".$onlyconsonants."'";
                           }

                        }


                    $comma_fields_order = "`".implode("`,`", $fields_order)."`";
                    $valuestring = implode(",", $valuesd );

                    $wpdb->query("INSERT INTO rets_property_2 (".$comma_fields_order.") VALUES (".$valuestring.")");

                        unset($valuesd);
                        $valuesd = array();




                } /* end while */

        } /* end if of number of row */

答案 1 :(得分:0)

我建议使用 DBName 列名而不是SystemName作为数据库表列名。

我还建议使用PDO来准备/执行你的陈述。

以下示例可以帮助您:

<?php
$sysid = '1234'
$property_1 = '1111 main'
$stmt = $db->prepare("INSERT INTO table_name(`sysid`, `property_1`) VALUES( ?, ?)");
$stmt->execute(array($sysid, $property_1));