我希望我的头衔太模糊。我正在开发一个项目,用于在数据库中存储具有不同类型的对象。该数据库由两个表组成,一个用于对象,另一个用于不同的类型。一切正常,除了我的html表单似乎无法通过复选框存储多个类型。当我在使用表单时检查多个框时,只会检查最后一个框。
我可以在一个通过Workbench手动执行SQL的对象上存储多个类型,但表单似乎没有发送正确的东西。我肯定做错了什么。有人可以帮帮忙吗?提前谢谢。
以下是生成表单的函数:
/**
* Content creation form
**/
public function formCreateObjectContent($output){
$sql = '
SELECT *
FROM `Genre`
';
$res = $this->db->ExecuteSelectQueryAndFetchAll($sql);
$html="
<div class='wrapper'>
<div id='object-edit'>
<form method='post'>
<fieldset>
<legend>Create new object</legend>
<p><label>Title:<br/><input type='text' name='title' required/></label></p>
<p><label>Cover-image:<br/><input type='text' name='image' value='default.png'/></label></p>
<ul>";
foreach($res as $val) {
$html .= "<li><input type='checkbox' name='genre' value='{$val->id}'/>$val->name</li>";
}
$html .= "</ul>
<p><label>Start Date:<br/><input type='text' name='start_date' value='0000-00-00'/></label></p>
<p><label>End Date:<br/><input type='text' name='end_date' value='0000-00-00'/></label></p>
<p><input type='submit' name='save' value='Save'/> <input type='reset' value='Reset'/></p>
<output><p>{$output}</p></output>
</fieldset>
</form>
</div>
</div>
";
return $html;
}
这是SQL的函数:
/**
* Create content
**/
public function createObjectContent($title, $genre, $image, $start_date, $end_date, $created, $save){
$output = null;
if($save) {
// Insert content to database.
$sql = '
INSERT INTO `Object` (`title`, `image`, `start_date`, `end_date`, `created`) VALUES
(?, ?, ?, ?, NOW())
';
$params = array($title, $image, $start_date, $end_date);
$res = $this->db->ExecuteQuery($sql, $params);
if($res) {
// Get id of the created Object.
$sql2 = '
SELECT *
FROM `Object`
ORDER BY `created` DESC
LIMIT 1;
';
$params2 = array();
$res2 = $this->db->ExecuteSelectQueryAndFetchAll($sql2, $params2);
foreach($res2 AS $val){
$idObject = $val->id;
}
// Match object id with genre id.
$sql = '
INSERT INTO `Object2Genre` (`idObject`, `idGenre`)
VALUES (?, ?);
'
;
$url = empty($url) ? null : $url;
$params = array($idObject, $genre);
$res = $this->db->ExecuteQuery($sql, $params);
}
// Return result.
if ($res){
$output = header('Location:browse.php');
}
else {
$output = "<p>Object could not be created.</p>" . print_r($this->db->ErrorInfo(), 1) . '</pre>';
}
}
return $output;
}
答案 0 :(得分:1)
您需要更改HTML代码并将[]添加到复选框名称:
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if( isset($_POST['submit']) )
{
//to run PHP script on submit
if( !empty($_POST['check_list']) )
{
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected)
{
echo $selected."</br>";
}
}
}
?>