我写了以下内容来从单选按钮(var1)读取数据和一些复选框(var2)
require('config.php');
$con = mysql_connect("localhost", $db_username, $db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $con);
$var1 = $_GET['var1'];
$var1 = mysql_real_escape_string( $var1 );
$var2 = $_GET['var2'];
$var2 = mysql_real_escape_string( $var2 );
$running_total = 0;
$last_update = "SELECT * FROM $db_tablename ORDER BY id DESC LIMIT 1";
$result = mysql_query($last_update);
$insert_query = "INSERT INTO $db_tablename (var1, var2) ";
$insert_query .= sprintf( "VALUES('%s', '%s')", $var1, $var2);
$insert_result = mysql_query($insert_query);
if(!$insert_result) {
die('insert query failed' . mysql_error());
}
mysql_close($con)
输入html看起来像......
<input type="radio" name="var1" value="value1">Test 1</input>
<input type="radio" name="var1" value="value2">Test 2</input>
<input type="radio" name="var1" value="value3">Test 3</input>
<input type="radio" name="var1" value="value4">Test 4</input>
<input type="checkbox" name="var2" value="checkbox1">CB 1</input>
<input type="checkbox" name="var2" value="checkbox2">CB 1</input>
<input type="checkbox" name="var2" value="checkbox3">CB 1</input>
<input type="checkbox" name="var2" value="checkbox4">CB 1</input>
一切都按预期工作,但由于var2允许选择多个复选框,我想保存多个值。我意识到我需要将其作为一个数组来完成,但我无法解决它。
任何人都可以帮助或找到他们可以指向我的解决方案或示例吗?
答案 0 :(得分:2)
var2[]
为name
并将结果视为
foreach($_POST['var2'] as $checkedItem)
{
echo $checkedItem;
}
答案 1 :(得分:1)
html: `
<input type="radio" name="var1" value="value1">Test 1</input>
<input type="radio" name="var1" value="value2">Test 2</input>
<input type="checkbox" name="var2[]" value="check" /> CB 1
<input type="checkbox" name="var2[]" value="check" /> CB 2
`
和SQL:
Array $_POST['var2'] contains the checkboxes.
答案 2 :(得分:0)
将var1的名称更改为“var1 []”,并将var2的名称更改为“var2 []”...
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];
echo count($var1);
echo count($var2);
gl mf