Php 将多个复选框值写入数据库
我正在尝试将多个复选框值写入数据库中以逗号分隔的字段,但它不起作用。我正在使用emplode和explode,但它只写入一个选定值。这意味着如果我选择2个选项,它将只写一个。下面是我的代码:Php 将多个复选框值写入数据库,php,html,mysql,Php,Html,Mysql,我正在尝试将多个复选框值写入数据库中以逗号分隔的字段,但它不起作用。我正在使用emplode和explode,但它只写入一个选定值。这意味着如果我选择2个选项,它将只写一个。下面是我的代码: <?php ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); session_start(); include_once '..../..php'; $emailtem
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();
include_once '..../..php';
$emailtemp = $_POST['email'];
$ordernumber2 = substr($emailtemp, 0, 3);
$ordernumber1 = RAND(1,100);
$ordernumber = $ordernumber2.$ordernumber1;
error_reporting(E_ALL);
ini_set('display_errors', '1');
$mysql_host = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$services = implode(",",$_POST["services"]);
$mysqli = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
$statement = $mysqli->prepare("INSERT INTO guest_orders (streetaddress, floor, building, apartment, area, phone, block, avenue, ordernumber, email, service) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$statement->bind_param('sssssssssss', $_POST['streetaddress'], $_POST['floor'], $_POST['building'], $_POST['apartment'], $_POST['area'], $_POST['phone'], $_POST['block'], $_POST['avenue'], $ordernumber, $_POST['email'], $services);
$results = $statement->execute();
if($results){
header('location:../thankyou.php');
}else{
header('location:../error.php');
}
?>
此外,我的表格:
<form method="post" action="includes/guestorder.php" name="GuestForm" id="GuestForm" autocomplete="off" onsubmit="return Confirm('Are you sure of the details entered?');">
<br>
<center>
Please choose the services you require<br><br>
<input name="services[]" type="checkbox" value="electrician" checked> Electrician <br>
<input name="health" type="checkbox" value="health"> Health Technician <br>
<input name="services[]" type="checkbox" value="ac"> Air Conditioning Technician <br>
<input name="computer" id="computer" type="checkbox" value="computer"> Computer Technician <br>
<input name="services[]" type="checkbox" value="satelite"> Satelite Service Technician <br>
<input name="services[]" type="checkbox" value="blacksmith"> Blacksmith <br>
<input name="services[]" type="checkbox" value="aliminium"> Aliminium Technician <br>
<input name="services[]" type="checkbox" value="carpenter"> Carpenter <br>
<input name="services[]" type="checkbox" value="housedecor"> Interior Designer & House Decorations <br>
<input name="services[]" type="checkbox" value="gardener"> Gardener <br>
<input name="services[]" type="checkbox" value="ceramics"> Ceramics <br>
<input name="services[]" type="checkbox" value="painter"> Painter <br>
<?php
echo "<br><fieldset>";
echo "<legend>Your Details & Information</legend>";
echo "Street Address:<br /> <input name='streetaddress' id='streetaddress' type='text' required /><br />";
echo "Building/House:<br /> <input name='building' id='building' type='text' required /><br />";
echo "Avenue:<br /> <input name='avenue' id='avenue' type='text'><br />";
echo "Block:<br /> <input name='block' id='block' type='text' required /><br />";
echo "Floor:<br /> <input name='floor' id='floor' type='text'><br />";
echo "Apartment:<br /> <input name='apartment' id='apartment' type='text'><br />";
echo "Area:<br /> <input name='area' id='area' type='text' required /><br />";
echo "Email Address:<br /> <input name='email' id='email' type='text' pattern='[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$' required /><br />";
echo "Phone Number:<br /> <input name='phone' id='phone' type='text' pattern='[569][0-9]{7}' required /><br />";
echo "</fieldset>";
?>
<br>
<input type="submit" class="submit" id="submit" value="Submit" /><input type="reset" value="Reset"><br>
</form>
请选择您需要的服务
电工
健康技师
空调技师
计算机技术员
卫星服务技术员
铁匠
营养素技师
木匠
室内设计师和房屋装饰
园丁
陶瓷
画家
将其与“.$services.”等$services的引号一起使用
将其与“.$services.”等$services的引号一起使用
我不确定你想要实现什么,因为留给我的是另一张桌子,上面有所有服务,然后是另一张桌子,上面保存了与所选服务相匹配的客人ID,但如果我们想查看你拥有的,请引用你的列表。
最好是单引号
$statement->bind_param('sssssssssss', $_POST['streetaddress'],
$_POST['floor'], $_POST['building'], $_POST['apartment'], $_POST['area'],
$_POST['phone'], $_POST['block'], $_POST['avenue'], $ordernumber,
$_POST['email'], "'".$services."'");
我不确定你想要实现什么,因为留给我的是另一张桌子,上面有所有服务,然后是另一张桌子,上面保存了与所选服务相匹配的客人ID,但如果我们想查看你拥有的,请引用你的列表。
最好是单引号
$statement->bind_param('sssssssssss', $_POST['streetaddress'],
$_POST['floor'], $_POST['building'], $_POST['apartment'], $_POST['area'],
$_POST['phone'], $_POST['block'], $_POST['avenue'], $ordernumber,
$_POST['email'], "'".$services."'");
第一部分是一个单独的PHP文件吗?它有多个ini设置和错误报告行。为什么要在那里使用这么多Hi呢!我修改了你的一些格式,使它更容易阅读。请注意在文章中保持均匀的间距,因为这样可以更容易地理解正在发生的事情,在你的脚本中也是如此!第一部分是一个单独的PHP文件吗?它有多个ini设置和错误报告行。为什么要在那里使用这么多Hi呢!我修改了你的一些格式,使它更容易阅读。请注意在文章中保持均匀的间距,因为这样可以更容易地理解正在发生的事情,在你的脚本中也是如此!bind_param
函数需要一个实际的PHP变量
,而不是一个值(表达式)。mysqli
中没有与之相当的bindValue
(PDO)。i、 e.除非提供变量列表,否则它将不起作用。bind_param
函数需要一个实际的PHP变量
,而不是一个值(表达式)。mysqli
中没有与之相当的bindValue
(PDO)。i、 e.除非提供变量列表,否则它将不起作用。