2015-09-30 55 views
1

我試圖將多個複選框值寫入數據庫中由逗號分隔的字段,但它不起作用。我正在使用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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="health" type="checkbox" value="health"> Health Technician &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="ac"> Air Conditioning Technician&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="computer" id="computer" type="checkbox" value="computer"> Computer Technician&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="satelite"> Satelite Service Technician&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="blacksmith"> Blacksmith &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="aliminium"> Aliminium Technician &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="carpenter"> Carpenter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="housedecor"> Interior Designer & House Decorations&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="gardener"> Gardener&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="ceramics"> Ceramics&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> 
    <input name="services[]" type="checkbox" value="painter"> Painter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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> 
+0

第一部分是否意味着一個單獨的PHP文件?它有多個ini_set和error_reporting行。 –

+0

爲什麼使用這麼多          –

+0

你好!我修正了一些格式,使其更易於閱讀。請注意在帖子中保持均勻間隔,因爲它可以更容易理解發生的情況,在腳本中也是如此! – SuperBiasedMan

回答

0

與報價$般的服務使用它

$statement->bind_param('sssssssssss', 
$_POST['streetaddress'],$_POST['floor'], $_POST['building'], 
$_POST['apartment'], $_POST['area'], $_POST['phone'], $_POST['block'], 
$_POST['avenue'], $ordernumber, $_POST['email'], '"'.$services.'"'); 
+0

'bind_param'函數需要一個實際的'PHP變量'而不是一個值(表達式)。 'mysqli'中沒有'bindValue'(PDO)等價物。即除非提供變量列表,否則它將不起作用。 –

0

不確定你想要達到什麼目的,因爲留給我,我會在另一張桌子上提供所有服務的建議,然後第二張桌子上有你的客人ID與選定的服務匹配保存,但如果我們想要按照你所擁有的,引用你的l IST。 最好是單引號。

$statement->bind_param('sssssssssss', $_POST['streetaddress'], 
$_POST['floor'], $_POST['building'], $_POST['apartment'], $_POST['area'], 
$_POST['phone'], $_POST['block'], $_POST['avenue'], $ordernumber, 
$_POST['email'], "'".$services."'"); 
相關問題