我試圖將多個複選框值寫入數據庫中由逗號分隔的字段,但它不起作用。我正在使用emplode並爆炸,但它只寫入一個選定的值。意思是如果我選擇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._%+-][email protected][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>
第一部分是否意味着一個單獨的PHP文件?它有多個ini_set和error_reporting行。 –
爲什麼使用這麼多 –
你好!我修正了一些格式,使其更易於閱讀。請注意在帖子中保持均勻間隔,因爲它可以更容易理解發生的情況,在腳本中也是如此! – SuperBiasedMan