我正在尝试将多个复选框值写入数据库中用逗号分隔的字段,但它不起作用。我正在使用WIP和爆炸但它只写了一个选定的值。这意味着如果我选择2个选项,它只会写一个。以下是我的代码:
<?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>
答案 0 :(得分:0)
将其与报价一起使用,例如&#39;&#34;&#39;。$ services。&#39;&#34;&#39;
$statement->bind_param('sssssssssss',
$_POST['streetaddress'],$_POST['floor'], $_POST['building'],
$_POST['apartment'], $_POST['area'], $_POST['phone'], $_POST['block'],
$_POST['avenue'], $ordernumber, $_POST['email'], '"'.$services.'"');
答案 1 :(得分:0)
不确定你想要实现什么,因为留给我,我会在另一张桌子上提供所有服务的建议,然后是第二张桌子,你可以用匹配的选定服务保存客人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."'");