gpt4 book ai didi

php - 将多个复选框值插入 mysql 字段但在每个复选框值之间添加空格?

转载 作者:行者123 更新时间:2023-11-30 00:16:36 24 4
gpt4 key购买 nike

我允许我的用户将一个或 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";
}
?>

最佳答案

只需使用:

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

而不是

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

关于php - 将多个复选框值插入 mysql 字段但在每个复选框值之间添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23607626/

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