2017-06-17 71 views
2

我正嘗試創建一個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的各種數據,就會產生一個輸入「姓名」,另一個輸入「結果」。

其實它的工作很好,但在我的SQL表,我看到這樣的插入的數據,如果我有兩個兩個輸入(姓名和結果):namenameresultresult

我想一樣,在對其進行格式化我sql表:

<p>Name : Result</p> 
<p>Name : Result</p> 

有誰知道我該怎麼做?

謝謝:)

+0

您確定要插入HTML標記嗎?你應該在輸出時間這樣做。 HTML/PHP/MySQL都是完全不同的動物,應該這樣對待。 –

+0

謝謝你的回覆,你能否給我一個例子在輸出期間做出來?謝謝:) – GeGeek

回答

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> 

OR

$textResult = "<p>".$name[$i]." : ".$result[$i]."</p>"; 

如果您必須添加標記

+0

你好,謝謝,它的工作! :)現在我有另一個問題,它只顯示一行,第三... – GeGeek

+0

我不明白你的意思 – hazelcodes

相關問題