2013-10-05 155 views
0

一直試圖將複選框的值存入數據庫但失敗, 任何人都有關於如何將複選框的值存入我的datbasen的提示?複選框到數據庫的值

然後有個問題,我怎樣才能在同一個文件中運行post操作?

我這麼久代碼:

<?php 
$con=mysqli_connect("db_info"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$sql="INSERT INTO mass (gift, name, epost, interest) 
VALUES 
('$_POST[gift_nor]','$_POST[name_nor]','$_POST[epost_nor]','$_POST[interest_nor]')"; 


if (!mysqli_query($con,$sql)) 
    { 
    die('Error: ' . mysqli_error($con)); 
    } 
echo "1 record added"; 

mysqli_close($con); 
?> 


<form method="post" action=""> 

    <input type="radio" name="gift_nor" value="present_1">present_1<br> 
    <input type="radio" name="gift_nor" value="present_2">present_2<br /> 
    <input type="radio" name="gift_nor" value="present_3">present_3 

    <hr /> 

    Namn: <input type="text" name="name_nor"> 
    Epostadress: <input type="text" name="epost_nor"> 

    <hr /> 

    <input type="checkbox" name="interest_nor[]" value="int_1">I have a bike<br> 
    <input type="checkbox" name="interest_nor[]" value="int_2">I have a car<br /> 
    <input type="checkbox" name="interest_nor[]" value="int_3">I have a computer 

    <hr />   

    <input name="Send" type="submit" id="Skicka"> 
</form> 
+0

[插入複選框值與PHP MySQL數據庫(HTTP的可能重複://計算器。問題/ 9142860 /插入 - 複選框 - 值 - 到 - mysql的數據庫與php) –

回答

3

使用implode, 做這樣的事情

<?php 
if (isset($_POST['Send'])) 
{ 
$con=mysqli_connect("db_info"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    $check=implode(',', $_POST['interest_nor']); //it convert checkbox array into string 

    $sql="INSERT INTO mass (gift, name, epost, interest) 
    VALUES 
    ('$_POST[gift_nor]','$_POST[name_nor]','$_POST[epost_nor]','$check')"; 
if (!mysqli_query($con,$sql)) 
    { 
    die('Error: ' . mysqli_error($con)); 
    } 
echo "1 record added"; 

mysqli_close($con); 
} 
?> 
+0

在代碼中我應該寫它? – lab

+0

看到我更新的答案... – Dinesh

+0

它現在將值保存在數據庫中,但當我訪問表單所在的頁面時可能會出現此錯誤:Warning:implode()[function.implode]:傳入的無效參數/storage/content/67/130567/s10.se/public_html/db.php 18行 – lab