1

Hi In the below I am creating group chat application for that I am passing the below details using action=createGroup from client side.

java

public String CreateGroup(String groupname,String username,
            ArrayList<FriendInfo> result) throws UnsupportedEncodingException  {

         List<String> usersName = new ArrayList<String>(); 
            for (int i = 0; i < result.size(); i++) { 
            usersName.add(result.get(i).userName); 
            }
                String params = "groupname="+ URLEncoder.encode(groupname,"UTF-8") +
                        "&username="+ URLEncoder.encode(this.username,"UTF-8") +
                        "&password="+ URLEncoder.encode(this.password,"UTF-8") +
                        "&friendUserName=" +usersName+      
                        "&action="  + URLEncoder.encode("CreateGroup","UTF-8")+
                        "&";

            Log.i("PARAMS", params);
            return socketOperator.sendHttpRequest(params);

This is my server side code.Where I did mistake because data not saving into database.Can any one please help me

php

   case "CreateGroup":
    $userId = authenticateUser($db, $username, $password);

    if ($userId != NULL)

    {

        if (isset($_REQUEST['friendUserName']))         
        {               
             $friendUserNames = $_REQUEST['friendUserName'];
            $friendUserNames = str_replace('[','',$friendUserNames);
            $friendUserNames = str_replace(']','',$friendUserNames);
            $friendUserNames = explode(",", $friendUserNames);
            foreach($friendUserNames as $friendUserName){

             $groupname = $_REQUEST['groupname'];

             $sql = "select Id from users where username='$friendUserName' limit 1";
             echo $sql;
             if ($result = $db->query($sql))

             {
                    if ($row = $db->fetchObject($result))

                    {
                         $requestId = $row->Id;

                         $groupname = $row->Id;

                         if ($row->Id != $userId)
                         {
                                 $sql = "insert into group (groupname,providerId, requestId)values(".$groupname.",".$userId.",".$requestId.")";
                                 echo $sql;


                                 if ($db->query($sql))
                                 {
                                        $out = SUCCESSFUL;
                                 }
                                 else
                                 {
                                        $out = FAILED;
                                 }
                        }
                        else
                        {
                            $out = FAILED;  
                        }                                                
                    }
                    else
                    {
                        $out = FAILED;                      
                    }
             }

             else
             {
                    $out = FAILED;
             }              
        }
        }
        else
        {
                $out = FAILED;
        }           
    }
    else
    {
        $out = FAILED;
    }   
break;

Table

CREATE TABLE IF NOT EXISTS `group` (
`id` int(11) NOT NULL,
  `groupname` varchar(25) NOT NULL,
  `providerId` int(25) NOT NULL,
  `requestId` int(5) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `group`
--
ALTER TABLE `group`
 ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `username` (`providerId`), ADD UNIQUE KEY `userid` (`providerId`), ADD UNIQUE KEY `ufriendid` (`requestId`);
15
  • what's the output of second echo $sql; ?? Commented Feb 6, 2015 at 6:21
  • @noob Second echo not printing anything it's giving FAILED Commented Feb 6, 2015 at 6:23
  • how abot first echo $sql;?? Commented Feb 6, 2015 at 6:24
  • @noob select Id from users where username='[user3,user1] Commented Feb 6, 2015 at 6:25
  • 1
    @user1: You are passing total array as a string to this query. The format how you are sending is wrong. It should come like: select Id from users where username='user3' Commented Feb 6, 2015 at 6:29

1 Answer 1

1

$_REQUEST['friendUserName'] is posting more than one username(an array). So you need to handle it properly

try replacing:

$friendUserName = $_REQUEST['friendUserName'];

with

$friendUserNames = $_REQUEST['friendUserName'];
$friendUserNames = str_replace('[','',$friendUserNames);
$friendUserNames = str_replace(']','',$friendUserNames);
$friendUserNames = explode(",", $friendUserNames);
foreach($friendUserNames as $friendUserName){
//your rest of code as it is 
} //and end tag for for loop at appropriate position from your code before the last second else
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.