入力に同じ名前を使用して特定の列にデータを挿入するPHPフォームを作成しようとしています。同じ名前を使用して複数の入力を使用してSQLでデータをフォーマットする
フォームは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="[email protected]">';
echo '<input class="uk-input" type="text" name="name[]" value="' . $exercice['name'] . '" readonly>';
echo '</div>';
echo '<div class="[email protected]">';
echo '<input class="uk-input" type="text" name="duration[]" value="' . $exercice['duration'] . '" readonly>';
echo '</div>';
}
else {
echo '<div class="[email protected]">';
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="[email protected]">';
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の様々なデータがあるとして、それは入力「名前」と別の入力「結果」を生成します。
実はそれは素晴らしい働いているが、私は(名前と結果)の両方のための2つの入力を持っている場合は、私のSQLテーブルで、私はこのような挿入されたデータを参照してください。namenameresultresult
私は私の中にそのようにそれをフォーマットしたいと思いますSQLテーブル:
<p>Name : Result</p>
<p>Name : Result</p>
どのようにすればよいのですか?
感謝:)
HTMLマークアップを挿入してもよろしいですか?出力時にこれを行う必要があります。 HTML/PHP/MySQLはまったく異なる動物であり、そのように扱うべきです。 –
お返事ありがとうございます。出力中に作成する例を教えていただけますか?ありがとう:) – GeGeek