2014-03-27 30 views
0
我有一個問題路過的名稱相同的輸入值,這是工作,但現在塔只是不工作再​​下面是HTML和PHP

提交值

HTML:

<form action="alterado.php" method="post"> 
<p class="codigo"><input type="text" name="codigo-peca[]"></p> 
<p class="descricao-peca"><input type="text" name="descricao-peca[]"></p> 
<p class="valor-peca">R$<input type="text" name="valor-peca[]"></p> 

<p class="codigo"><input type="text" name="codigo-peca[]"></p> 
<p class="descricao-peca"><input type="text" name="descricao-peca[]"></p> 
<p class="valor-peca">R$<input type="text" name="valor-peca[]"></p> 

<input type="submit" value="Salvar Dados"> 

alterado.php

if(is_array($_POST['valor-peca']) && is_array($_POST['codigo-peca']) && is_array($_POST['descricao-peca']) ) { 
    // ... code 
    for($i = 0; $i < count($_POST['valor-peca']); $i++) { 
     // ... reference index of arrays 
     $valorPeca = $_POST['valor-peca'][$i]; 
     $codigoPeca = $_POST['codigo-peca'][$i]; 
     $descricaoPeca = $_POST['descricao-peca'][$i]; 

     if ($valorPeca != 0){ 
     $SQL = "INSERT INTO pecas (ordemServico, codigoPeca, descricaoPeca, valorPeca) VALUES ('$ordem', '$codigoPeca', '$descricaoPeca', '$valorPeca');"; 
$result = mysql_query($SQL); 
} 
     } 
    } 

但這是僅讀取第一輸入值而不是第二

+0

**危險**:您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068)並應使用[現代替換](http ://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin

+0

我不認爲這是問題,但檢查是否會有任何區別,如果您將value =「」添加到輸入標記。 –

+0

我檢查了米羅斯拉夫,但沒有工作要麼 – Bertucci

回答

0

您正在覆蓋變量$valorPeca$codigoPeca$descricaoPeca。它只會一次顯示一個變量,除非將它們附加在一起。

if(is_array($_POST['valor-peca']) && is_array($_POST['codigo-peca']) && is_array($_POST['descricao-peca']) ) { 
      $valorPeca = ''; 
      $codigoPeca = ''; 
      $descricaoPeca = '' 
      for($i = 0; $i < count($_POST['valor-peca']); $i++) { 
       // ... reference index of arrays 
       $valorPeca .= $_POST['valor-peca'][$i] . " "; 
       $codigoPeca .= $_POST['codigo-peca'][$i] . " "; 
       $descricaoPeca .= $_POST['descricao-peca'][$i] . " "; 
      } 
} 
+0

對不起,我沒有發佈一切,我用它輸入數據庫,所以我只需要一次,但每當我添加查詢保存,只保存第一,我現在發佈完整的PHP代碼 – Bertucci