在几个 Stack Overflow 成员的指导下,我得到了一个 PHP 表单来与我的 MySQL 数据库进行通信。现在,我为我的复选框使用了一个数组,并且信息确实到达了 MySQL 表……有点……
下面的代码会将所有记录插入数据库,但复选框对应的列具有值“数组”而不是分配的值...:
<?php
function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="stylized" class="myform">
<form id="form" name="form" action="" method="post">
. . .
<input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" />
. . .
<footer><input type="submit" name="submit" value="Add this Article"></footer></form>
</div>
</body>
</html><?php
}
. . .
if(count($articletags) > 0)
{
$articletags_string = implode(",", $articletags);
}
if (isset($_POST['submit']))
{
$articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
$articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
$articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
$articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
$articletags = ($_POST['articletags']);
. . .
mysql_query("INSERT articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' ")
or die(mysql_error());
header("Location:addsuccess.html");
}
}
else
{
renderForm('','','','');
}
?>