如何将这些字符串数据从 PHP 插入 MySQL 表,我可以让代码运行但它不插入数据库。
php代码:
<?php
//mysql connection
$connect_error = 'Sorry, the database server is down. Please try again later.';
mysql_connect('xxxxxxxxx.com', 'xxxxxxx', 'xxxxxxxxx') or die($connect_error);
mysql_select_db('checkbox_test') or die($connect_error);
if (isset($_POST['submit'])) {
if (isset($_POST['subject'])) {
$strsubject = implode(",", ($_POST['subject']));
} else {
$strsubject = "";
}
echo $strsubject;
}
mysql_query("INSERT INTO levels (level_1, level_2, level_3, level_4, level_5, level_6) VALUES ($strsubject");
?>
的HTML:
<form action="" method="post" name="form_subject" id="form_subject">
<input type="checkbox" name="subject[]" value="level_1"><label>Level 1</label><p>
<input type="checkbox" name="subject[]" value="level_2"></label>Level 2</label><p>
<input type="checkbox" name="subject[]" value="level_3"></label>Level 3</label><p>
<input type="checkbox" name="subject[]" value="level_4"><label>Level 4</label><p>
<input type="checkbox" name="subject[]" value="level_5"></label>Level 5</label><p>
<input type="checkbox" name="subject[]" value="level_6"></label>Level 6</label><p>
<input type="submit" action= "" name="submit" value="Submit" />
</form>
数据库表有 7 列,1 列用于 id(设置为 A_I),其余为每个级别命名。我是网络开发新手,谢谢大家