使用explode数组发布到数据库

时间:2014-03-19 15:17:29

标签: jquery sql autocomplete

我所拥有的是JQuery和AutoComplete。一切都很好,直到我尝试发布到SQL ...

我将城市名称,州名缩写和邮政编码拉入自动填充程序,这就是编码...

目前我正在显示一个像这样的空间

  

Springfield Mo 65803

但如果城市名称有2个单词,那么

  

得梅因IA 50301

由于分隔符而导致错误...我可能会将分隔符设为逗号但在网站上看起来不太好...

<?php
if (is_numeric($_REQUEST['term']))
{
$dblink = mysql_connect('localhost', 'DBUser', 'DBpass') or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

$rs = mysql_query('select StateInitials, City, ZipCode from locations where ZipCode like "'. mysql_real_escape_string($_REQUEST['term']) .'%" GROUP BY StateInitials, City', $dblink);

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['City'] .' '. $row['StateInitials'] .' '. $row['ZipCode'],
            'value' => $row['City'] .' '. $row['StateInitials'] .' '. $row['ZipCode']
        );
    }
}

echo json_encode($data);
flush();
}
else
{
if ( !isset($_REQUEST['term']) )
    exit;

$dblink = mysql_connect('localhost', 'DBUser', 'DBpass') or die(mysql_error());
mysql_select_db("DBName") or die(mysql_error());

$rs = mysql_query('select StateInitials, City, min(ZipCode) AS "Lowest Zip" from locations where City like "'. mysql_real_escape_string($_REQUEST['term']) .'%" GROUP BY StateInitials, City', $dblink);

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['City'] .' '. $row['StateInitials'] .' '. $row['Lowest Zip'],
            'value' => $row['City'] .' '. $row['StateInitials'] .' '. $row['Lowest Zip']
        );
    }
}

echo json_encode($data);
flush();
}
?>

你可以看到我有自动填充城市名称或邮编

这是我的编码将它分开并保存为LocationID

    if(isset($_POST["OriginCombo"]))
        {
            $Origin  = $_POST["OriginCombo"];
                $pieces = explode(" ", $Origin);
                $OriginCity = $pieces[0];
                $OriginState = $pieces[1];
                $OriginZip = $pieces[2];


        $location_id = $this->Origin->GetLocationID($OriginState, $OriginCity, $OriginZip);
        if ($location_id == -1)
        {
            // error
            die("origin error");
        }
        else if (is_array($location_id))
            $location_id = $location_id[0];
        $this->Origin->LocationID = $location_id;

        }

1 个答案:

答案 0 :(得分:1)

考虑到zip最后改变为

//first step
                $zip = array_pop($pieces);
                $state = array_pop($pieces);

                //second step
                $city = implode(" ",$pieces);

                $OriginCity = $city
                $OriginState = $state;
                $OriginZip = $zip;

或更好,如果你使用字符串函数和类似的逻辑$ _POST [“OriginCombo”]值

*编辑**

我在这样的测试中尝试了这个,它显示了城市的数组

<?php
$_POST["DestCombo"] = "Des Moines ID 76789";

                    $Dest  = $_POST["DestCombo"];
                    $pieces = explode(" ", $Dest);              
                //  $DestCity = $pieces[0];
                //  $DestState = $pieces[1];
                //  $DestZip = $pieces[2];

                $zip = array_pop($pieces);
                $state = array_pop($pieces);

                //second step
                $city = implode(" ", $pieces);

                $DestCity = $city;
                $DestState = $state;
                $DestZip = $zip;

                echo $DestCity;
                echo "<br>";
                echo $DestState;
                echo "<br>";
                echo $DestZip;
                ?>

输出:

警告:explode()期望参数2为字符串,在第14行的/home/SiteName/public_html/test.php中给出数组

ID 76789