我正在尝试创建一个PHP表单,以便在我的输入中使用相同的名称在特定列中插入数据。
通过从SQL请求数据来动态创建表单,这就是每个输入具有相同名称的原因。
这是我的代码:
<?php
if(!empty($_POST)){
$name = $_POST['name'];
$description = $_POST['description'];
$result = $_POST['result'];
$db = Database::connect();
$datenow = date("Y-m-d H:i:s");
$start = $db->prepare("INSERT INTO trainings (user,program,date) values(?, ?, ?)");
$start->execute(array($userId,$id,$datenow));
$getid = $db->query("SELECT tid AS trainingid FROM trainings WHERE user = $userId AND tid = LAST_INSERT_ID()");
$trainingIds = $getid->fetch();
$trainingLastId = $trainingIds['trainingid'];
for($i = 0; $i < count($_POST['result']); $i++){
$textResult = array_merge($name,$result);
$text = implode($textResult);
$insert = $db->prepare("UPDATE trainings SET trainings.results = '$text' WHERE tid = $trainingLastId");
$insert->execute();
}
Database::disconnect();
//header("Location: index.php");
} ?>
形式:
$db = Database::connect();
global $userId;
$exercices = $db->query("SELECT exercices.id, exercices.user, exercices.program, exercices.name, exercices.type, exercices.duration, programs.id AS program FROM exercices LEFT JOIN programs ON exercices.program = programs.id WHERE exercices.program = $id AND exercices.user = '" . $userId . "'");
$nexercices = $exercices->fetch();
if ($nexercices['program'] == 0){
echo "Nothing";
}
else {
$listExercices = $db->query("SELECT exercices.name, exercices.description, exercices.type, exercices.duration FROM exercices WHERE program = $id AND user = '" . $userId . "'");
$listExercice = $listExercices->fetchAll();
echo '<form method="post" action="" class="uk-grid-small" uk-grid>';
foreach($listExercice AS $exercice){
if($exercice['type'] == 'Repos'){
echo '<div class="uk-width-1-2@s">';
echo '<input class="uk-input" type="text" name="name[]" value="' . $exercice['name'] . '" readonly>';
echo '</div>';
echo '<div class="uk-width-1-2@s">';
echo '<input class="uk-input" type="text" name="duration[]" value="' . $exercice['duration'] . '" readonly>';
echo '</div>';
}
else {
echo '<div class="uk-width-1-2@s">';
echo '<input class="uk-input" type="text" name="name[]" value="' . $exercice['name'] . '" readonly>';
echo '<input class="uk-input uk-form-blank" name="description[]" value="' . br2nl($exercice['description']) . '" readonly>';
echo '</div>';
echo '<div class="uk-width-1-2@s">';
echo '<p>Résultat</p>';
echo '<input class="uk-input" type="text" name="result[]" value="">';
echo '</div>';
}
}
echo '<button type="submit" class="uk-button uk-button-primary">Create</button>';
echo '</form>';
}
Database::disconnect();
由于SQL中有各种数据,它将生成输入“name”和另一个输入“result”。
实际上它工作得很好,但是在我的SQL表中,如果我有两个输入(名称和结果),我会看到插入的数据:namenameresultresult
我想在我的sql表中格式化它:
<p>Name : Result</p>
<p>Name : Result</p>
有谁知道我该怎么办?
谢谢:)
答案 0 :(得分:0)
将for
循环修改为如下所示:
for($i = 0; $i < count($_POST['result']); $i++){
$textResult = $name[$i]." : ".$result[$i];
$insert = $db->prepare("UPDATE trainings SET trainings.results = '$textResult' WHERE tid = $trainingLastId");
$insert->execute(); }
并在<p>
标记中回显:
<p><?php echo $yourrecord; ?> </p>
或强>
$textResult = "<p>".$name[$i]." : ".$result[$i]."</p>";
如果必须添加标记