2017-06-01 44 views
0

朋友們,我將字段名稱顯示爲數據庫的值1和值作爲字段的結果,但我無法提高它以保存字段的值。更新while和php

$result = $db->query($query); 

    while($rowID = $result->fetchArray()){ 
    echo 
    " <label class=\"control-label \" for=\"text\"> 
     ".$row['value1'] ." 
     </label> 
     <input class=\"form-control\" value=".$rowID['value2'] ." id=\"\" name=".$rowID['id']." type=\"text\"/> 

    </div>"; 
    } 

if(isset($_POST['submit_data'])){ 

     $ret1 = $db->query($query); 
     while($rowID = $ret1->fetchArray()) 
     {   

     $query = "UPDATE table set value2 ='" . $_POST[$rowID['id']] . "' WHERE id='" . $rowID['id'] . "'"; 


      } 

     if($db->exec($query)){ 
      $message = "Data is updated successfully."; 
     }else{ 
      $message = "Sorry, Data is not updated."; 
     } 
    } 
+0

您顯示的是value1,並存儲了value2。 –

+0

http://bobby-tables.com - 瞭解SQL注入。您的腳本處於非常高風險之中,您的數據庫可能會在幾秒鐘內被黑客入侵! – Twinfriends

回答

1

您在while循環中最後生成的值將是$ query的值。你應該同時執行查詢。

if(isset($_POST['submit_data'])){ 

    $ret1 = $db->query($query); 
    while($rowID = $ret1->fetchArray()) 
    {   

     $query = "UPDATE table set value2 ='" . $_POST[$rowID['id']] . "' WHERE id='" . $rowID['id'] . "'"; 
     if($db->exec($query)){ 
      $message = "Data is updated successfully."; 
     }else{ 
      $message = "Sorry, Data is not updated."; 
     } 

    } 


}