简体   繁体   中英

records are not saving into database using php in android

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`);

$_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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM