duanhuang1967 2013-01-21 23:17
浏览 54
已采纳

将多个复选框值插入单个SQL数据库列

I'm looking for a simple solution to inserting multiple checkbox selections into a single database column. User selects box 1, 4 and 6 - so the database would reflect "1, 4, 6".. the commas would be nice but are not neccessary. If one checkbox is selected, the information will insert as it should. If multiple are selected, only the last one will be inserted into the DB. Everything else is working fine, but these darned checkboxes. I tried to use

<?php
if(isset($_POST['mode'])) {
    $mode = implode(",", $_POST['mode']);   
} else {
    $mode = "";
}
?>

<?php

as suggested by another for someone asking the same question, though this method leaves me with "Array" inside of the column instead of any numbers no matter what is selected.

I tried to post some pictures to better explain, but it will not allow me. I'm no SQL or PHP professional, just trying to learn as much as I need to get this database going fairly quickly. If there is a solution to this issue, please enlighten me.

Here is the code I am using for the action script on submit:

<?php
if(isset($_POST['mode'])) {
$mode = implode(",", $_POST['mode']);   
} else {
$mode = "";
}
?>

<?php

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$gamename=$_POST['gamename'];
$region=$_POST['region'];
$mode=$_POST['mode'];
$notes=$_POST['notes'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(gamename, region, mode, notes)VALUES('$gamename', '$region', '$mode', '$notes')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?> 

<?php 
// close connection 
mysql_close();
?>

And here is the code for my check boxes:

<td><input type="checkbox" name="mode[]" id="mode" value="1" />
 mode 1<br />
<input type="checkbox" name="mode[]" id="mode" value="2" />
  mode 2<br />
<input type="checkbox" name="mode[]" id="mode" value="3" />
  mode 3<br />
<input type="checkbox" name="mode[]" id="mode" value="4" />
  mode 4 <br /> 
<input type="checkbox" name="mode[]" id="mode" value="5" />
  mode 5 <br />
<input type="checkbox" name="mode[]" id="mode" value="6" />
  mode 6 <br />
<input type="checkbox" name="mode[]" id="mode" value="7" />
  mode 7<br />
<input type="checkbox" name="mode[]" id="mode" value="8" />
  mode 8
  </td>

As a side note, what steps should I take to ensure my DB and website are not vulnerable to simple exploits. I plan for these forms to be used by the public to openly submit information to the database.

  • 写回答

2条回答 默认 最新

  • douyabu1528 2013-01-21 23:27
    关注

    you override correct $mode variable, that's why you see Array in your table data.

    remove this line from code:

    $mode=$_POST['mode'];
    

    and about vulnerable: you should escape string this way:

    $gamename = mysql_escape_string($_POST['gamename']);
    $region = mysql_escape_string($_POST['region']);
    $notes = mysql_escape_string($_POST['notes']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器