Php 在mysql字段中插入多个复选框值,但在每个值之间添加空格?

Php 在mysql字段中插入多个复选框值,但在每个值之间添加空格?,php,mysql,Php,Mysql,我允许我的用户在数据库的一个字段中插入一个或4个复选框值,以后我希望能够通过对每个词进行查询来检索这些数据,但是,当在我的字段中插入多个值时,它会用逗号分隔这些值,这很好,但是如何用空格分隔这些词呢?因此,我可以稍后在查询中搜索包含“mechanical”或“non_mechanical”等的值 下面是它的存储方式 mechanical,non_mechanical 此外,逗号是否会阻止我对所有包含“mechanical”值的对象进行查询,例如,因为它的末尾会有一个逗号 html: mysql

我允许我的用户在数据库的一个字段中插入一个或4个复选框值,以后我希望能够通过对每个词进行查询来检索这些数据,但是,当在我的字段中插入多个值时,它会用逗号分隔这些值,这很好,但是如何用空格分隔这些词呢?因此,我可以稍后在查询中搜索包含“mechanical”或“non_mechanical”等的值

下面是它的存储方式

mechanical,non_mechanical
此外,逗号是否会阻止我对所有包含“mechanical”值的对象进行查询,例如,因为它的末尾会有一个逗号

html:

mysql:

<?php
session_start();

$db_hostname = 'localhost';
$db_database = 'xxxx'; 
$db_username = 'xxxx';
$db_password = 'xxxx';

$db_server = mysql_connect($db_hostname, $db_username, $db_password)    
        or die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database)   
or die("Unable to select database: " . mysql_error());

 $scope_type = implode(",",$_POST["scope_type"]);

    $ipaddress = $_SERVER["REMOTE_ADDR"];




$sql="INSERT INTO supplier_scope (user_ip, scope_type)
    VALUES ('$ipaddress', '$scope_type')";  

$result = mysql_query($sql); 

$sql2="UPDATE supplier_session SET form2_completed = 'Yes' WHERE form2_completed = 'No' AND user_IP = '$ipaddress'";

$result2 = mysql_query($sql2); 


 if($result){

    header("Location: index.php?registration=success");

}else {
echo "ERROR";
}
?>
只需使用:

$scope_type = implode(" ",$_POST["scope_type"]);
而不是

$scope_type = implode(",",$_POST["scope_type"]);

若你们的值在一列中用逗号分隔,你们仍然可以在表中进行搜索

为此

select * 
from TABLENAME 
where FIND_IN_SET('mechanical', COLUMNNAME) 
在表中存储分隔值也不是很好。看看这个


您可以使用replace函数来使用空格而不是逗号。

为什么不使用json来存储您的复选框值?我从未听说过它,也不知道如何使用它:只需在php中使用json\u encode$\u POST[scope\u type]
select * 
from TABLENAME 
where FIND_IN_SET('mechanical', COLUMNNAME)