这是我用来尝试创建多行以插入数据库的表单。
<input name="input[]" type="checkbox" value="0" id="input_0" /> Directional<br /> <br/>
<input name="input[]" type="checkbox" value="1" id="input_1" /> Technical <br />
<br />
<input name="input[]" type="checkbox" value="2" id="input_2" /> Reference <br />
<br />
<input name="input[]" type="checkbox" value="3" id="input_3" /> Research <br />
<br />
<input name="input[]" type="checkbox" value="4" id="input_4" /> Phone <br /></h3>
<input type="submit" name="button" id="button" value="Submit Tally" style="height: 25px; width: 100px">
</Form>
使用 implode 从复选框表单将多行插入 mysql 数据库的当前问题。循环可以解决这个问题吗?选中复选框时我可以看到数组 (#,#,#,#)
$type = $_POST['input'];
// $sid = $_SERVER['REMOTE_ADDR'];
$user = $_POST['user'];
if(count($type) > 0)
{
$type_string = implode(',', $type);
}
$sql = "INSERT INTO tally (tid, sid, uid, date, time, catid)
VALUE (NULL, 1, '$user', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '$type_string')";
mysql_query($sql) or die(mysql_error());
echo "Success";
print_r($type_string);
最佳答案
在您从作为输入收到的 $type 变量创建一些数组并将其一次输入到您的查询中之后,循环肯定会起作用。
但我希望您不要像这样在某些网站中使用此代码。正如 maiotano84 已经评论的那样,您创建查询的方式有点危险。很容易对您的数据做一些不需要的事情。至少你应该把你的变量通过一个像
这样的函数string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier = NULL ] )
这里是关于这个函数的更多信息,来自 php.net网站。
关于php - 使用 PHP/SQL 插入多行时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12539194/