2014-10-18 89 views
0

我需要插入多行數據到表 日期和inspection_id將保持不變 但輸入值重複。 這裏是腳本我有插入多行到一個表使用foreach php

  <?php 
if (isset($_POST["submit"])) { 
$taskdate = date('Y:m:d'); 
$inspection_id = '1'; 
$post = $_POST['nfo']; 
foreach ($post['point_id'] as $key => $value) { 
$point_id.= $value.", "; 
} 
foreach ($post['point_comment'] as $key => $value) { 
$point_comment.= $value.", "; 
} 

foreach ($post['point_value'] as $key => $value) { 
$point_value.= $value.", "; 
} 
$query = "INSERT INTO `inspections` (`inspection_id`, `point_id`, `value`, `comment`)VALUES('$inspection_id', '$point_id', '$point_value', '$point_comment')"; 
$result = mysql_query($query); 

HTML表單我使用

          <form action="pentasks.php" method="post"> 
                <select name="nfo[point_value][]"> 
                 <option selected>Chose</option> 
                  <option value="1">Qualify</option> 
                  <option value="2">Disqualify</option> 
                </select> 
                 <input name = "nfo[point_comment][]" value = "" type="text"> 
                 <input type="hidden" name="inspection_id" value="<?= $task_id; ?>"> <!-- value From another query --> 
                 <input type="hidden" name="nfo[point_id][]" value="<?= $spot_id3; ?>"> 

        <input type="submit" name="submit" value="Submit"> 
       </form> 

請幫助我如何將數據插入 這個東西在一個表中的問題被定義 在表單中檢索與輸入答案

+0

你是不是做是正確的。你的代碼很容易受到sql注入的影響 – 2014-10-18 13:09:16

+0

爲什麼你用數組命名你的輸入名稱爲'nfo [point_value] []'。那只是一批輸入 – Ghost 2014-10-18 13:10:31

回答

0

試試這個

foreach ($post['point_comment'] as $key => $value) { 
$point_comment=$post['point_comment'][$value]; 
$point_value= $post['point_value'][$value]; 
//run your query here 
} 

和MySQL是depricated瞭解mysqli_功能或PDO.

,改變你的選擇標記這樣

<select name="point_value[]"> 
<select name="point_comment[]"> 
+0

警告:爲foreach()提供的參數無效2:未定義索引:point_comment – iCodeCrew 2014-10-18 15:41:36

+0

其次,類似問題的大部分答案太複雜了: – iCodeCrew 2014-10-18 15:46:01

0

阿里夫感謝你的努力,但你的回答沒有幫助 通過ownself我得到它的工作花費幾個小時這裏是我的問題的可行的解決方案

HTML表單

<input type = "text" name = "point_comment[]" /> 
<input type = "text" name = "point_value[]" /> 

這裏的foreach循環PHP

$my_comment = $_POST['point_comment']; 
$point_comment = ""; 
foreach ($my_comment as $key => $value) { 
$point_comment = $value; 

$my_value = $_POST['point_value']; 
$point_value = ""; 
foreach ($my_value as $key => $value) { 
$point_value = $value; 
// SQL INSERT or anything query here 
} } 

希望這將有助於爲那些尋找類似