2014-01-24 37 views
0

我想在一次提交中更新表格行。但我不知道該怎麼做。更新提交中的多行

這是我的代碼

<form method="POST" action="<?php $_PHP_SELF ?>"> 
    <div class="col-md-7" style="margin-left:370px; height:auto;"> 
    <h3>Service-Level-Agreement</h3> 
    <?php 

     $query = "select * from tblsla"; 
     $request = mysql_query($query)or die(mysql_error());?> 
     <table class="table table-hover" style="width:auto;"> 
     <tr>  
       <th>Category</th> 
       <th>Easy</th> 
       <th>Moderate</th> 
       <th>Difficult</th> 
       <th></th> 
       </tr> 
     <?php while($row = mysql_fetch_array($request)){ 
     ?> 
     <tbody> 
      <tr> 
       <td><?php echo $row['category']; ?></td> 

       <td><input type="text" class="form-control" style="width:50px;" name="easySla"/></td> 
       <td><input type="text" class="form-control" style="width:50px;" name="modSla"/></td>  
       <td><input type="text" class="form-control" style="width:50px;" name="difSla"/></td>  
        <td>Days</td> 
      </tr> 
     </tbody> 
     <?php 
     } 

?> 
    </table> 
    <input type="submit" name="submit" id="submit" value="Edit SLA" class="btn btn-success"/> 
    </div> 

     <input type="hidden" name="idUser" value="<?php echo $row['id'];?>" /> 
    </form> 

PHP

<?php 
    $easy = $_POST["easySla"]; 
$mod = $_POST["modSla"]; 
$diff = $_POST["diffSla"]; 


if(is_int($easy) AND is_int($mod) AND is_int($diff)){ 
    echo " Invalid Inputs "; 
    } 
else 
    { 
    $sql = "UPDATE tblsla SET easySLA = '$easy', modSLA = '$mod', difSLA = '$diff' WHERE id = '$id'"; 
if(isset($_POST['submit'])){ 
$success = mysql_query($sql) or die (mysql_error()); 
} 
if ($success == TRUE){ 
    ?> 
     <script> 
      alert('You have successfully update account.'); 
     </script> 

    <?php 
} 
    } 

?> 

這是像什麼,我希望發生的This is what im trying to do.

太謝謝你了!

+0

你有severals輸入具有相同的名稱。你需要使用數組作爲輸入名稱。 –

回答

1

你有severals輸入具有相同名稱的
使用數組中你輸入名字

嘗試是這樣的:

<form method="POST" action="<?php $_PHP_SELF ?>"> 
     <div class="col-md-7" style="margin-left:370px; height:auto;"> 
      <h3>Service-Level-Agreement</h3> 
      <?php 

      $query = "select * from tblsla"; 
      $request = mysql_query($query)or die(mysql_error());?> 
      <table class="table table-hover" style="width:auto;"> 
       <tr> 
        <th>Category</th> 
        <th>Easy</th> 
        <th>Moderate</th> 
        <th>Difficult</th> 
        <th></th> 
       </tr> 
       <?php while($row = mysql_fetch_array($request)){ 
        ?> 
        <tbody> 
        <tr> 
         <td><?php echo $row['category']; ?></td> 

         <td><input type="text" class="form-control" style="width:50px;" name="easySla[]"/></td> 
         <td><input type="text" class="form-control" style="width:50px;" name="modSla[]"/></td> 
         <td><input type="text" class="form-control" style="width:50px;" name="difSla[]"/></td> 
<td> <input type="hidden" name="idUser[]" value="<?php echo $row['id'];?>" /></td> 
         <td>Days</td> 

        </tr> 
        </tbody> 
       <?php 
       } 

       ?> 
      </table> 

      <input type="submit" name="submit" id="submit" value="Edit SLA" class="btn btn-success"/> 
     </div> 


    </form> 

在你的php中,你有一個包含所有行的數組。你需要遍歷這個數組並在數據庫中插入數據。

嘗試這樣的:

if (isset($_POST['submit'])) { 
$count = count($_POST['easySla']); //get total number of array element 
for ($i = 0; $i < $count; $i++) { // loop through array and assign values in variable and insert itin database 
    $easy = $_POST['easySla'][$i]; 
    $mod = $_POST["modSla"][$i]; 
    $diff =$_POST["difSla"][$i]; 
    $id = $_POST["idUser"][$i]; 
    if (is_numeric($easy) && is_numeric($mod) && is_numeric($diff)) { 

     $sql = "UPDATE tblsla SET easySLA = '$easy', modSLA = '$mod', difSLA = '$diff' WHERE id = '$id'"; 
     $success = mysql_query($sql) or die (mysql_error()); 
     if ($success == false) { 
      break; 
     } 
    } else { 

     echo " <script> 
       alert('invalid input not a number.'); 
       </script>"; 
     header("location: yourfile_name.php"); 
    } 
} 
if ($success) { 
    echo " <script> 
     alert('You have successfully update account.'); 
    </script>"; 
    header("location: yourfile_name.php"); 
} else { 
    echo " <script> 
    alert('You have errors.'); 
    </script>"; 
    header("location: yourfile_name.php"); 
} 

}

讓我知道如果您有任何疑問..

+0

它不工作.. – rapidoodle

+0

我正在更新我的答案。給我一點時間 –

+0

好吧謝謝你! – rapidoodle

0

您可以使用循環與不同數據或相同的數據更新表中的所有行...

如果你有不同的數據對所有行採取數組中的數據,並開始與該數組循環長度,在該循環中寫入查詢以更新每行中的數據。

例:$陣列= [[ROW1] [ROW2] [ROW3]] 爲長度$數組{ $查詢=,ROW2 = 「更新表組ROW1 ='ROW1的數據」'數據的環路2' 行的......「enter code here }

+0

你能舉個例子嗎? – rapidoodle