Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将多个值添加到单行MYSQL/PHP中,并分开_Php_Mysql_Mysqli - Fatal编程技术网

将多个值添加到单行MYSQL/PHP中,并分开

将多个值添加到单行MYSQL/PHP中,并分开,php,mysql,mysqli,Php,Mysql,Mysqli,我正在尝试创建一个注册用户可以加入“旅行”的系统。一旦他提交了想要加入的信息,他的用户id将被捕获并发送到数据库中名为usersJoined的列中。其思想是多个用户可以加入“trip”,所有id都存储在一个单元格中,并用逗号分隔。为了稍后检索id,我将使用explode函数 我的问题是,最后附加的id也有一个逗号。explode函数将最后一个逗号后的空格视为“object”,它将存储在捕获explode函数的数组中 有没有办法确保最后附加的id没有逗号? 或者你有其他方法的建议吗 这是我的密码:

我正在尝试创建一个注册用户可以加入“旅行”的系统。一旦他提交了想要加入的信息,他的用户id将被捕获并发送到数据库中名为usersJoined的列中。其思想是多个用户可以加入“trip”,所有id都存储在一个单元格中,并用逗号分隔。为了稍后检索id,我将使用explode函数

我的问题是,最后附加的id也有一个逗号。explode函数将最后一个逗号后的空格视为“object”,它将存储在捕获explode函数的数组中

有没有办法确保最后附加的id没有逗号? 或者你有其他方法的建议吗

这是我的密码:

function joinPost($id, $s_email){

    // Captures the user ID from a SESSION variable received outside this function
    $query = "SELECT `id` FROM `users` WHERE `email` = '$s_email'";
    $result = $this->db->query($query);

    if ($result->num_rows > 0) {        
        while($row = $result->fetch_array()) {
            $userID = $row['id'];
        }
    }
    $userIDwithComma = $userID . ','; // <-- this should be different, I guess 

    // Check if user joined already
    $query = "SELECT `usersJoined` FROM `trips` WHERE id = '$id'";
    $result = $this->db->query($query);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_array()) {      
            $usersJoined = $row['usersJoined'];
        }
    }

    $users = explode(',', $usersJoined);
    $check = 0;

    foreach($users as $user){
        if($user == $userID){
            $check = 1;
        }
    }

    if($check != 1){
        $sql = "UPDATE trips SET usersJoined = CONCAT(usersJoined, ?) WHERE id = '$id'";

        if($stmt = $this->db->prepare($sql)) {
            $stmt->bind_param('s', $userIDwithComma);
            $stmt->execute();

            if($stmt->fetch()) {
                $stmt->close();
                return true;
            }
            return true;
        }
    } else {
        header('Location: trips.php?status=already_joined');
    }
}
函数joinPost($id,$s_电子邮件){
//从该函数外部接收的会话变量捕获用户ID
$query=“从`users`中选择`id`,其中`email`='$s_email';
$result=$this->db->query($query);
如果($result->num_rows>0){
而($row=$result->fetch_array()){
$userID=$row['id'];
}
}
$userIDwithComma=$userID.',';//数据库->查询($query);
如果($result->num_rows>0){
而($row=$result->fetch_array()){
$usersJoined=$row['usersJoined'];
}
}
$users=explode(“,”,$usersJoined);
$check=0;
foreach($users作为$user){
如果($user==$userID){
$check=1;
}
}
如果($check!=1){
$sql=“UPDATE trips SET usersJoined=CONCAT(usersJoined,?),其中id='$id';
如果($stmt=$this->db->prepare($sql)){
$stmt->bind_param('s',$userIDwithComma);
$stmt->execute();
如果($stmt->fetch()){
$stmt->close();
返回true;
}
返回true;
}
}否则{
标题('Location:trips.php?status=ready_joined');
}
}


必须删除最后一个逗号

更改此项:

$stmt->bind_param('s', $userIDwithComma);
致:


哦,我的。。。你的数据库设计得很糟糕。你应该建立一种多对多的关系。现在,您有两个表:
Users
Trips
。您应该使用FK
user\u id
trip\u id
创建一个新表,如下所示:

UsersGoTrip
=====================
| user_id | trip_id | 
=====================
|       1 |       1 |
|       2 |       1 |
|       1 |       2 |
=====================

然后,加上,删除和选择前往特定旅行或特定用户正在参加的旅行的用户只需一个查询即可。

为什么不使用单独的表并使用联接处理查询?@RowlandShaw你的确切意思是什么?只需将数据存储在适当的规范化设计中。我的意思是与@barbarity所做的类似建议作为回答您好,谢谢您的评论。我是一名初级PHP程序员,所以任何建议都非常感谢:)我将尝试在谷歌上搜索更多关于这种方法的信息。谢谢,我们是来帮忙的。一旦你读到这篇文章,你会问自己,你怎么能用其他的方法来做呢。我保证。如果你觉得这个答案有帮助,请把它投上一票,并把它标记为正确的,这样其他有问题的人会更容易找到答案。
$stmt->bind_param('s', $userIDwithComma);
$stmt->bind_param('s', substr($userIDwithComma, 0, -1));
UsersGoTrip
=====================
| user_id | trip_id | 
=====================
|       1 |       1 |
|       2 |       1 |
|       1 |       2 |
=====================