gpt4 book ai didi

php - 记录没有保存到数据库中使用 php 在 android

转载 作者:行者123 更新时间:2023-11-30 02:21:56 25 4
gpt4 key购买 nike

您好,在下面我正在创建群聊应用程序,我正在使用客户端的 action=createGroup 传递以下详细信息。

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

这是我的服务器端代码。我哪里出错了,因为数据没有保存到数据库中。任何人都可以帮助我

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;

表格

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'] 发布了多个用户名(一个数组)。所以你需要妥善处理

尝试替换:

$friendUserName = $_REQUEST['friendUserName'];

$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

关于php - 记录没有保存到数据库中使用 php 在 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com