无法将数据插入远程数据库

时间:2015-05-07 11:09:51

标签: php ios

我正在使用以下代码进行客户端编码。它没有按照我想要的方式工作,也没有将数据保存到远程服务器。如果我将链接直接传递给Web浏览器,那么它将数据保存到远程数据库。

客户端:

-(IBAction)Save:(id)sender {
        NSString *insrt=[NSString stringWithFormat:@"http://localhost/basInsertv1.php?Zipcode=%i&Locality=%@&Propertytype=%@&pricerange=%@&Bedrooms=%i&Bathrooms=%i&Possession=%@&Propertyage=%@&Rating=%@&Parking=%i&Powerbackup=%i&Security=%i&Swimmingpool=%i",zipcodetf.text.intValue,localitytf.text,proprtytypetf.text,sliderresltlabel.text,bedroomstf.text.intValue,bathrooms.text.intValue,possesiontf.text,propertyAge.text,rating.text,parking.text.intValue,pbu.text.intValue,sec.text.intValue,swim.text.intValue];
        NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:insrt]];
        NSString *r=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",r);
    }

服务器端:

 <?php

        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = '';
        $db = 'BuyAndSell';
        //$dbtable ='bas';
        // Connect Database
        $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error());
        mysql_select_db($db, $conn) or die(mysql_error());
        if (isset ($_GET['Zipcode']) && isset ($_GET['Locality']) && isset ($_GET['Propertytype']) && isset ($_GET['pricerange']) && isset ($_GET['Bedrooms']) && isset ($_GET['Bathrooms']) && isset ($_GET['Possession']) && isset ($_GET['Propertyage']) && isset ($_GET['Rating']) && isset ($_GET['Parking']) && isset ($_GET['Powerbackup']) && isset ($_GET['Security']) && isset ($_GET['Swimmingpool']))
        {
            $zipcode=$_GET['Zipcode'];
            $locality=$_GET['Locality'];
            $propertytype=$_GET['Propertytype'];
            $pricerange=$_GET['pricerange'];
            $bedrooms=$_GET['Bedrooms'];
            $bathrooms=$_GET['Bathrooms'];
            $possession=$_GET['Possession'];
            $propertyage=$_GET['Propertyage'];
            $rating=$_GET['Rating'];
            $parking=$_GET['Parking'];
            $powerbackup=$_GET['Powerbackup'];
            $security=$_GET['Security'];
            $swimmingpool=$_GET['Swimmingpool'];
        }

         else
        {
         echo "failed";
        }

            $sql ="INSERT INTO bas(id, zipcode, locality, propertytype,Pricerange,bedrooms,bathrooms,possession,propertyage,rating,parking,powerbackup,security,swimmingpool)VALUES(NULL,'$zipcode','$locality','$propertytype','$pricerange','$bedrooms', '$bathrooms', '$possession', '$propertyage','$rating','$parking','$powerbackup','$security','$swimmingpool');";

            $res = mysql_query($sql,$conn) or die(mysql_error());
            if ($res)
            {
                echo "Success";
            }
            else
            {
                echo "faild to insert";
            }

      /*  }
        else
        {
            echo "successrr";

    }*/
    ?>

这是正确的打印failedSuccess。问题出在我的客户端代码中。它不会将数据存储在远程数据库中。

2 个答案:

答案 0 :(得分:0)

请通过以下代码行更改您的插入字符串,这可能会有所帮助:

   NSString *insrt=[NSString stringWithFormat:@"http://localhost/basInsertv1.php?Zipcode=%i&Locality=%@&Propertytype=%@&pricerange=%@&Bedrooms=%i&Bathrooms=%i&Possession=%@&Propertyage=%@&Rating=%@&Parking=%i&Powerbackup=%i&Security=%i&Swimmingpool=%i",zipcodetf.text.intValue,localitytf.text,proprtytypetf.text,sliderresltlabel.text,bedroomstf.text.intValue,bathrooms.text.intValue,possesiontf.text,propertyAge.text,rating.text,parking.text.intValue,pbu.text.intValue,sec.text.intValue,swim.text.intValue];

    NSString *newInsrStr = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:0)

我认为这是因为你正在使用&#34; localhost&#34;在您的网址中。 尝试为您的服务器输入正确的IP地址(例如192.168.0.1) 提示:127.0.0.1不正确。

您还可以将服务器上的所有访问记录到文件中,或者在实际调用该站点时检入服务器的日志。

您的服务器代码太糟糕了 我仍然不喜欢它,但尝试这样:

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $db = 'BuyAndSell';
    //$dbtable ='bas';
    // Connect Database
    $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error());
    mysql_select_db($db, $conn) or die(mysql_error());
    if (isset ($_GET['Zipcode']) && isset ($_GET['Locality']) && isset ($_GET['Propertytype']) && isset ($_GET['pricerange']) && isset ($_GET['Bedrooms']) && isset ($_GET['Bathrooms']) && isset ($_GET['Possession']) && isset ($_GET['Propertyage']) && isset ($_GET['Rating']) && isset ($_GET['Parking']) && isset ($_GET['Powerbackup']) && isset ($_GET['Security']) && isset ($_GET['Swimmingpool']))
    {
//remember to escape received data!
        $zipcode=$_GET['Zipcode'];
        $locality=$_GET['Locality'];
        $propertytype=$_GET['Propertytype'];
        $pricerange=$_GET['pricerange'];
        $bedrooms=$_GET['Bedrooms'];
        $bathrooms=$_GET['Bathrooms'];
        $possession=$_GET['Possession'];
        $propertyage=$_GET['Propertyage'];
        $rating=$_GET['Rating'];
        $parking=$_GET['Parking'];
        $powerbackup=$_GET['Powerbackup'];
        $security=$_GET['Security'];
        $swimmingpool=$_GET['Swimmingpool'];

        $sql ="INSERT INTO bas(id, zipcode, locality, propertytype,Pricerange,bedrooms,bathrooms,possession,propertyage,rating,parking,powerbackup,security,swimmingpool)VALUES(NULL,'$zipcode','$locality','$propertytype','$pricerange','$bedrooms', '$bathrooms', '$possession', '$propertyage','$rating','$parking','$powerbackup','$security','$swimmingpool');";

        // insert into database only if all parameters are given
        $res = mysql_query($sql,$conn) or die(mysql_error());
        if ($res) {
            echo "Success";
        }
        else {
            echo "faild to insert";
        }   
    }
    else {
    echo "failed - data missing";
    }

我还建议使用POST发送数据 - 服务器对GET数据长度有限制