2012-01-30 82 views
0

我想創建一個表格爲我的俱樂部,它需要從數據庫中的信息,使從數據庫中的學員的子選擇。然後另外從事件列表中選擇一個並將其插入到數據庫中。它寫入數據庫確定,依次通過正確的次數,但犯規通$見習值到數據庫 我認爲失敗是信息的傳遞從多輸入表格不傳遞信息到mysql數據庫

print ' <input type="hidden" name="Trainee" value= ' . $trainee . ' /> 

到$查詢在if(isset($_POST['formSubmit'])) loop.

有人告訴我我要去哪裏嗎?下面列出的代碼

<?php 

//Retrieve trainees of specified grade 

$data = mysql_query('SELECT * FROM membership WHERE grade = "Trainee" ') 
or die(mysql_error()); // select works 

// Writes to database OK, including Trainee if manual value entered into form like done in instrucot 
    $query = "INSERT INTO testtraining (trainee_no, activity, instructor, entered_by, entered_by_date) VALUES ('{$_POST['Trainee']}', '{$_POST['activity']}', '{$_POST['instructor']}', '{$_POST['enteredBy']}', NOW())"; 

// Feedback and posting 
    if(isset($_POST['formSubmit'])) 

{ 
    $aTrainee = $_POST['data']; 
    $training = $_POST['activity']; 

    if(empty($aTrainee)) 
     { 
      echo("<p>You didn't select trainees.</p>\n");    
     } else { 
     $N = count($aTrainee); 
     echo("<p>You selected $N trainee(s): "); 

      for($i=0; $i < $N; $i++) // loop thru all selected checkbox adding 
       { 
        $trainee = $aTrainee[$i]; 
        // Execute the query. 
        if (@mysql_query ($query)) { 
         // lists OK on screen but does not pass to form for writing to database 
         print "<p>The $training added for $trainee.</p>"; 
            } 
       } 
     }         
} 
// end of posting 

// Start of form 
// Creates list with checkbox, cycles through info from membership database and makes a  multi select checkbox list 
while($info = mysql_fetch_array($data)) //repeat while there is still data from SELECT 
{ 
?> 
<form action ="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" > 
<input id= "<?= $info['no'] ?>" type="checkbox" name="data[]" value="<?= $info['no'] ?>" /> 
<label for="<?= $info['no'] ?>"><?= $info['_no'] ?></label> 
<br /> 
<? 
} 

// Training Activities checkbox, Displays training activity to be selected from 
print '<p><input type="radio" name="activity" value="Training1" /> Training1</p>';  //works 
print '<p><input type="radio" name="activity" value="Training2" /> Training2</p>'; //works 

print ' <input type="hidden" name="Trainee" value= ' . $trainee . ' /> 
<input type="hidden" name="instructor" value= anInstructor /> 
<input type="hidden" name="enteredBy" value=' . ($_SESSION['username']) . ' /> 
<input type="submit" name="formSubmit" value="Add Training" /> 
</form>'; 

mysql_close(); // Close the database connection; 
?> 

回答

0

您的查詢不會從字符串中跳出來插入變量。 而是嘗試:

$query = "INSERT INTO testtraining (trainee_no, activity, instructor, entered_by, entered_by_date) VALUES ('".$_POST['Trainee']."', '".$_POST['activity']."', '".$_POST['instructor']."','".$_POST['enteredBy']."', NOW())"; 

雖然我建議首先將這些$ _ POST變量到$變量,並運行一些驗證,以確保它是乾淨的。 addslashes()是確保不會彈出SQL錯誤的良好開端。但這不是關於安全地插入消毒用戶輸入的講座。

+0

感謝您的建議,結果相同。它仍然用/來填充數據庫,我認爲這是 user1177909 2012-01-31 12:29:32

相關問題