2017-01-06 151 views
1

我已經創建了一個包含兩組複選框並希望將它們存儲在MySQL數據庫中的表單,但是當我發佈到數據庫時,所有數據都按照預期如日期,文本和單選按鈕,但兩個文本框除外。如果我查看數據庫中存儲文本框值的列,它只會顯示「數組」並且沒有任何實際值。如何將複選框中的多個值添加到mysql數據庫中

這是我的代碼來處理POST請求:

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 


$name = $_POST['input4']; 
$datedone = $_POST['date']; 
$projectnumber = $_POST['input1']; 
$area = $_POST['input2']; 
$donebefore = $_POST['radio9']; 
$changesmade = $_POST['radio8']; 
$safeaccess = $_POST['radio11']; 
$electrical = $_POST['radio5']; 
$machineguarding = $_POST['radio6']; 
$correctequipment = $_POST['radio4']; 
$sds = $_POST['radio3']; 
$controltoxic = $_POST['radio1']; 
$ppe = $_POST['radio2']; 
$hazard = $_POST['checkbox']; 
$otherhazards = $_POST['input3']; 
$controlofhazards = $_POST['checkbox1']; 
$monitor = $_POST['radio12']; 
$comments = $_POST['input']; 

$sql = "INSERT INTO hira (Name, TodayDate, ProjectNumber, Area, DoneBefore, HaveChangesMade, SafeAccess, ElectricalEquipment, MachineGuarding, CorrectEquipment, SDS, ControlToxic, PPE, Hazard, OtherHazard, ControlHazard, MonitorProcess, AdditionalComments) VALUES ('$name','$datedone','$projectnumber','$area','$donebefore','$changesmade','$safeaccess','$electrical','$machineguarding','$correctequipment','$sds','$controltoxic','$ppe','$hazard','$otherhazards','$controlofhazards','$monitor','$comments')"; 

if ($conn->query($sql) === TRUE) { 
    echo "Thank you for completing the Hira form"; 

} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

$conn->close(); 
?> 

我做了什麼錯在這裏。我希望逗號分隔的文本框的值是「噪音,灰塵」 這是可能的嗎?提前

+0

print_r $ _POST你會得到它,出了什麼問題,相信我 – rahulsm

回答

0

感謝您可以使用implode功能

$checkbox = implode(",",$_POST['checkbox']); 
0

您可以使用使用插件組的implode功能,分隔字符串

$values = implode(",",$_POST['name']); 

可以使用explode函數來檢索字符串到陣列

$values = explode(",",$values); 
+0

謝謝你的幫助,但是當我使用 –

相關問題