Search code examples
phpmysqlsqliteappcelerator-mobile

How to insert data from sqllite database to remote mysql database using appcelerator titanium and php


I have tried using the following code. But it is not working. I have a temporary sqllite table, I need to insert all data from temporary database to remote mysql server.

var url = "http://bmcagro.com/manoj/insertopinion.php";
    var xhr = Ti.Network.createHTTPClient({
           onload: function(e) {
           // this.responseText holds the raw text return of the message (used for JSON)
           // this.responseXML holds any returned XML (used for SOAP web services)
           // this.responseData holds any returned binary data
           Ti.API.debug(this.responseText);
           var json = this.responseText;  
           var response = JSON.parse(json); 

           if (response.logged == "true") {
             var newtoast = Titanium.UI.createNotification({
                          duration: 1000,
                          message: "Inserted"
           });
           newtoast.show();
           } else {  
                   var toast = Titanium.UI.createNotification({
                   duration: 2000,
                   message: "False"
           });
           toast.show();
    }
    },
    onerror: function(e) {
        Ti.API.debug(e.error);
         var toast = Titanium.UI.createNotification({
                   duration: 2000,
                   message: "Error in Connection!!"
       });
       toast.show();
    },
    timeout:5000 });

    xhr.open("POST", url);
    xhr.send({names: names});   
 });

and the php code is

<?php
    $con = mysql_connect("MysqlSample.db.8189976.hostedresource.com","MysqlSample","xszZ@123ddlj");  
    if (!$con) {
        echo "Failed to make connection.";
        exit;
    }
    $db = mysql_select_db("MysqlSample",$con);
    if (!$db) {
        echo "Failed to select db.";
        exit;
    }  
    $names = $_POST['names'];
    foreach ($names as $name) {
    mysql_query("INSERT INTO seekopinion(uid,gid,opiniondescription,date,postedto) VALUES (" + $name.gid + "," + $name.tempid + "," + $name.gid + ",NOW()," + $name.gid + ")");
    }
    if($query)   {  
         $sql = "SELECT * FROM MysqlSample.seekopinion";  
         $q= mysql_query($sql); 
         $row = mysql_fetch_array($q);       
         $response = array(  
            'logged' => true, 
            'seekopinion' => $row['seekopinion']        
         );
        echo json_encode($response);  
     } else {  
             $response = array(  
                'logged' => false,  
                'message' => 'User with same name exists!!'  
            );  
        echo json_encode($response);    
    }
?>  

actually iam a beginer in php as well as titanium...anybody pls help me out.


Solution

  • Finally i found a way out .... I changed the entire row to a string using delimiter '-' in appcelerator and then passed the parameter to the php code...from where the code is split using explode and then inserted using for loop

    the appcelerator code for posting a table from an sqllite database to mysql database..

    postbutton.addEventListener('click', function(e) 
    {
    var names = [];
    var datarow ="";
    var db = Ti.Database.open('weather');
    var rows = db.execute('SELECT tempid,gid,name,email FROM postedto');
    while (rows.isValidRow())
    {
    datarow=datarow+"-"+rows.fieldByName('tempid')
    rows.next();
    }
    db.close();
    var params = {  
                     "uid": Ti.App.userid,       
                     "opiniondescription": question2.text, 
                     "database": datarow.toString()
                       };  
    
    var url = "http://asdf.com/as/asd.php";
    var xhr = Ti.Network.createHTTPClient({
               onload: function(e) {
               // this.responseText holds the raw text return of the message (used for JSON)
               // this.responseXML holds any returned XML (used for SOAP web services)
               // this.responseData holds any returned binary data
               Ti.API.debug(this.responseText);
               var json = this.responseText;  
               var response = JSON.parse(json); 
    if (response.logged ==true) 
    {
    var seekopinion11=require('seekopinion2');
    var seekop11 = new seekopinion11();
    var newWindow = Ti.UI.createWindow({
                    //fullscreen : true,
                    backgroundImage : 'images/background.jpg',
                    });
                    newWindow.add(seekop11);
                    newWindow.open({
                    //animated : true
    });
    }
    else  
    {  
       var toast = Titanium.UI.createNotification({
                   duration: 2000,
                   message: response.message
       });
       toast.show();
    }  
    },
    onerror: function(e) {
    Ti.API.debug("STATUS: " + this.status);
    Ti.API.debug("TEXT:   " + this.responseText);
    Ti.API.debug("ERROR:  " + e.error);
    var toast = Titanium.UI.createNotification({
                duration: 2000,
                message: "There was an error retrieving data.Please try again"
       });
       toast.show();
    },
    timeout:5000
    });
    
     xhr.open("GET", url);
     xhr.send(params);
    });
    

    the php code for breaking the string using explode

      <?php  
    $con = mysql_connect("MysqlSample.db.hostedresource.com","MysqlSample","xszZ@");  
    if (!$con)  
    {  
        echo "Failed to make connection.";  
        exit;  
    }  
    $db = mysql_select_db("MysqlSample",$con);  
    if (!$db)  
    {  
        echo "Failed to select db.";  
        exit;  
    }  
    $uid= $_GET['uid'];  
    $opiniondescription= $_GET['opiniondescription']; 
    $database= $_GET['database']; 
    $insert = "INSERT INTO seekopinion(uid,opiniondescription,date) VALUES ('$uid','$opiniondescription',NOW())";  
    $query= mysql_query($insert);  
    $rows = explode("-", $database);
    $arrlength=count($rows);
    for($x=0;$x<$arrlength;$x++)
    {
    $insert = "INSERT INTO seekopinionuser(sid,postedto) VALUES ((SELECT MAX(sid) FROM seekopinion),$rows[$x])";  
    $query= mysql_query($insert);  
    }
        if($query)  
        {  
         $sql = "SELECT s.sid,s.opiniondescription,s.uid,u.postedto FROM seekopinion  s left join seekopinionuser u on s.sid=u.sid WHERE uid=$uid AND s.sid=(SELECT MAX(sid) FROM seekopinion) ";  
         $q= mysql_query($sql); 
         $row = mysql_fetch_array($q);       
         $response = array(  
            'logged' => true, 
            'opiniondescription' => $row['opiniondescription'],
            'uid' => $row['uid'] ,
            'sid'=>$row['sid']
    
            );  
        echo json_encode($response);  
    
        }  
        else  
        {  
             $response = array(  
            'logged' => false,  
            'message' => 'Seek opinion insertion failed!!'  
        );  
        echo json_encode($response);    
        }  
    ?>