我允许我的用户在我的数据库中的一个字段中插入一个或4个复选框值,然后希望能够通过对每个单词执行查询来检索这些数据,但是,当一个以上的值插入到我的字段中时,它会用逗号分隔这些值,这很好,但是我如何用空格分隔这些单词?所以以后我可以在我的查询中搜索包含'mechanical‘或'non_mechanical’等的值。
下面是它的存储方式
mechanical,non_mechanical另外,逗号是否会阻止我查询所有包含'mechanical‘值的内容,例如,因为它的末尾会有一个逗号?
html:
<p>mechanical</p>
<input type="checkbox" id="box1" name="scope_type[]" value="mechanical" onClick="showMore(this);">
<p>Non-Mechanical Hire</p>
<input type="checkbox" id="box1" name="scope_type[]" value="non_mechanical" onClick="showMoreMore(this);">
<p>Tools</p>
<input type="checkbox" id="box1" name="scope_type[]" value="tools" onClick="showMoreMoreMore(this);">
<p>Accessories</p>
<input type="checkbox" id="box1" name="scope_type[]" value="accessories" onClick="showMoreMoreMoreMore(this);">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";
}
?>发布于 2014-05-12 19:25:15
只需使用:
$scope_type = implode(" ",$_POST["scope_type"]);而不是
$scope_type = implode(",",$_POST["scope_type"]);发布于 2014-05-12 19:31:20
但是,如果您的值在列中用逗号分隔,您仍然可以在表中进行搜索。
为了达到这个目的,
select *
from TABLENAME
where FIND_IN_SET('mechanical', COLUMNNAME) 另外,在表中存储分隔值也不是很好。看看这个。
Is storing a delimited list in a database column really that bad?
发布于 2014-05-12 19:36:28
您可以使用replace函数来使用空格而不是逗号。
https://stackoverflow.com/questions/23607626
复制相似问题