[英]Issue inserting multiple rows using PHP/SQL
This is the form in which I am using to try and create multiple rows to insert into a database. 这是我用来尝试创建要插入数据库的多行的表单。
<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>
Current issue of inserting multiple rows into mysql database from a checkbox form using implode. 当前问题是使用implode从复选框表单向mysql数据库中插入多行。 Would a loop work to fix this issue?
循环是否可以解决此问题? I can see the array (#,#,#,#) when check boxes are selected
选中复选框后,我可以看到数组(#,#,#,#)
$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);
A loop would certainly work, after you created some array from that $type variable you receive as input, and feed this into your query one at the time. 在您从该$ type变量创建了一些数组作为输入之后,一次将循环送入查询中肯定会起作用。
But I hope you're not going to use this code in some website just like this. 但我希望您不要像这样在某些网站中使用此代码。 As maiotano84 already commented, the way you create your query is a little dangerous.
正如maiotano84已经评论过的那样,创建查询的方式有些危险。 It is very easy to do some unwanted things with your data.
对您的数据进行一些不必要的操作非常容易。 At least you should put your variables through a function like
至少您应该将变量通过类似
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier = NULL ] )
Here is some more info on this function frm the php.net site. 这是php.net网站上有关此功能的更多信息。
compute the value of $type
as below: 计算
$type
的值,如下所示:
$i=0;
foreach ($_POST as $v) {
if(isset($v['input'.$i])) {
$type = $v;
$i++;
}
}
<Form action="" method="POST">
<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>
<?php
$type = $_POST['input'];
// $sid = $_SERVER['REMOTE_ADDR'];
$user = $_POST['user'];
for($i=0;$i<count($type);$i++){
$sql = "INSERT INTO tally (tid, sid, uid, date, time, catid)
VALUE (NULL, 1, '$user', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ".$type[$i].")";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.