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 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突