我有一个问题。我想制作一个历史时间表,在这个时间轴上用户可以添加'元素' (例如每年的元素。)。
在表单1中,用户创建时间轴并说明时间线上的元素数量,这些元素将被发送到数据库(这是有效的)。 在表单2中,用户可以填充元素。 (这不起作用)。
在表单1(如果有人想要5个元素)发布后,数据库会创建5条记录(每个元素一条)。
现在我想要5种不同的形式来填充这5个元素(标题,描述等),但是有1个提交按钮。
现在,如果我尝试这个(见下面的代码),只有第5个表格被发布... 我如何让它工作?
提前致谢,
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('classes/database.class.php');
$DB = Database::getInstance();
$titel = '';
$beschrijving = '';
$afbeeldingURL = '';
$jaar = '';
$element_id = '';
$velden = '';
$sql3 = "SELECT `id` , `aantal_elementen` FROM `tijdlijn` ORDER BY id DESC LIMIT 0, 1";
$last_id = $DB->_query($sql3);
if ($last_id->num_rows > 0) {
while($row = $last_id->fetch_assoc()) {
$last_id2 = $row["id"];
$aantal_elementen3 = $row["aantal_elementen"];
}
}
$sql4 = "SELECT * FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2."";
$sql5 = "SELECT `id` FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2." ORDER BY id ASC LIMIT 0,1";
$sql6 = "SELECT `id` FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2." ORDER BY id DESC LIMIT 0,1";
$sql7 = "SELECT `id` FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2." ORDER BY id ASC";
$last_id3 = $DB->_query($sql4);
$min_id = $DB->_query($sql5);
$min_id = mysqli_fetch_assoc($min_id);
$min_id = $min_id['id'];
$max_id = $DB->_query($sql6);
$max_id = mysqli_fetch_assoc($max_id);
$max_id = $max_id['id'];
$dingid[] = $DB->_query($sql7);
echo $min_id."</br>";
echo $max_id."</br>";
$current_id = $min_id;
if (isset($_POST) && !empty($_POST)) {
$boolError = false;
foreach ($_POST as $k => $v) {
$_POST[$k] = trim($v);
}
if (!isset($_POST['titel']) || trim($_POST['titel']) == '') {
$titel = 'error';
$boolError = true;
}
if (!isset($_POST['beschrijving']) || trim($_POST['beschrijving']) == '') {
$beschrijving = 'error';
$boolError = true;
}
if (!isset($_POST['afbeeldingURL']) || trim($_POST['afbeeldingURL']) == '') {
}
if (!isset($_POST['jaar']) || trim($_POST['jaar']) == '') {
$jaar_start = 'error';
$boolError = true;
}
if (
!isset($_POST['element_id'])
|| trim($_POST['element_id']) == ''
|| !preg_match('/^\d+$/', $_POST['element_id'])
) {
$element_id = 'error';
$boolError = true;
}
if ($boolError === false) {
$sql = "UPDATE `elementen`
SET
`titel` = '" . $_POST['titel'] . "',
`beschrijving` = '" . $_POST['beschrijving'] . "',
`afbeelding_url` = '" . $_POST['afbeeldingURL'] . "',
`jaar` = '" . $_POST['jaar'] . "'
WHERE `id` = ".$_POST['element_id'].";
";
if ($DB->_query($sql)) {
//$current_id++;
//exit;
} else {
header('Location: ?oops2');
exit;
}
} else {
$velden = "Ff alles invullen he";
}
?> <p>Vul het formulier hier onder in voor de elementen en maak een tijdlijn!</p><?php
}
if ($last_id3->num_rows > 0) {
while($row2 = $last_id3->fetch_assoc()) {
$last_id4 = $row2["id"];
?>
<form class="form-nieuws" method="post">
</br></br>
<?php echo $last_id2; ?>
<?php echo $current_id; ?>
<input id="titel2" class="<?= $titel ?> form-control" type="text" placeholder="Titel tijdlijn" name="titel" value="<?= isset($_POST['titel']) ? $_POST['titel'] : '' ?>">
<br>
<textarea id="beschrijving2" class="<?= $beschrijving ?> form-control" rows="3" placeholder="Beschrijving over tijdlijn" name="beschrijving" value="<?= isset($_POST['beschrijving']) ? $_POST['beschrijving'] : '' ?>"></textarea>
<br>
<input id="afbeeldingURL2" class="<?= $afbeeldingURL ?> form-control" type="text" placeholder="URL voor afbeelding" name="afbeeldingURL" value="<?= isset($_POST['afbeeldingURL']) ? $_POST['afbeeldingURL'] : '' ?>">
<br>
<input id="jaar" class="<?= $jaar ?> form-control" type="text" placeholder="Start jaar tijdlijn" name="jaar" value="<?= isset($_POST['jaar']) ? $_POST['jaar'] : '' ?>">
<input id="element_id" class="<?= $element_id ?> form-control" type="text" placeholder="<?php echo $last_id4 ?>" name="element_id" value="<?= isset($_POST['element_id']) ? $_POST['element_id'] : '' ?>">
<div class="foutlabel">
<em class="<?= ($velden) ? 'error' : '' ?>"><?= ($velden) ? $velden : '' ?>
</em>
</div>
</br>
<?php $current_id++;}
} ?> <div class="gabutton">
<input type="submit" value="Creeer tijdlijn">
</div>
</form>
答案 0 :(得分:3)
最好的方法是只为每个元素提供一个输入表单。您可以根据元素生成每个表单输入。
通过这种方式,您只有一个按钮和一个调用来将数据发送到您的服务器。
在服务器端,您只需要解析和检查数据,并注意您发送的元素数量。