2016-03-23 125 views
0

我用這個代碼的工作:更新陣列PDO

$sortId = $_POST['sortId']; 
$sortName = $_POST['sortName']; 
$sortType = $_POST['sortType']; 
$location = $_POST['location']; 
$active = $_POST['active']; 

$sql = "UPDATE `sorts` SET `sort` = :sort 
WHERE `sort_id` = :sort_id"; 

$stmt = $db->prepare($sql); 
$stmt->bindParam(':sort', $sort); 
$stmt->bindParam(':sort_id', $sort_id); 

$db->beginTransaction(); 

foreach($sortName as $i => $sort) 
{ 
    $sort_id = $sortId[$i]; 
    $stmt->execute(); 
} 
$db->commit(); 

它的工作原理,它應該讓我繼續添加

<div class="chuteAudit"> 
     <div> 
      <input type="hidden" name="sortId[]" value="<?php echo $name['sort_id']; ?>" > 
      <input type="text" name="sortName[]" value="<?php echo $name['sort']; ?>" > 
      <select name="sortType[]" > 
            <optgroup label="<?php echo $name['shipping_sort'] ?>"> 
             <option value="amzl[]">AMZL</option> 
             <option value="other[]">Other</option> 
            </optgroup> 
     </select> 
      <select name="location[]" > 
            <option value=""><?php echo $name['section'] ?></option> 
            <option value="chutes">Chutes</option> 
            <option value="spine">Spine</option> 
            <option value="RMspine">Royal Mail spine</option> 
            <option value="RMchutes">Royal Mail chutes</option> 
            <option value="exceptions">Exceptions</option> 
            <option value="teamLift">Team lift</option> 
      </select> 
      <select name="active[]" > 
            <option value=""><?php echo $name['active'] ?></option> 
            <option value="yes">Yes</option> 
            <option value="no">No</option> 
      </select> 
     </div> 

     </div> 

我曾以爲額外的價值

$sortId = $_POST['sortId']; 
    $sortName = $_POST['sortName']; 
    $sortType = $_POST['sortType']; 
    $location = $_POST['location']; 
    $active = $_POST['active']; 

    $sql = "UPDATE `sorts` SET `sort` = :sort, `shipping_sort` = :sortType 
    WHERE `sort_id` = :sort_id"; 

    $stmt = $db->prepare($sql); 
    $stmt->bindParam(':sort', $sort); 
    $stmt->bindParam(':sort_id', $sort_id); 
    $stmt->bindParam(':sortType', $sortType); 

    $db->beginTransaction(); 

    foreach($sortName as $i => $sort) 
    { 
     $sort_id = $sortId[$i]; 
     $stmt->execute(); 
    } 
    $db->commit(); 

HTML這會很好,因爲它只是增加了一些已經可以工作的東西,但它並不像我原先想象的那麼簡單是,目前我收到了錯誤信息Notice: Array to string conversion我知道它與我添加的附加代碼有關,但我不確定自己做錯了什麼。

回答

1

在您的表單中有sortType[]使您的$sortType爲數組。

你的陣列這裏綁定:

$stmt->bindParam(':sortType', $sortType); //$sortType is an array 

你所需要的結合更像是

$stmt->bindParam(':sortType', $sort_type); 

,然後添加到的foreach

foreach($sortName as $i => $sort) 
{ 
    $sort_id = $sortId[$i]; 
    $sort_type = $sortType[$i]; 
    $stmt->execute(); 
} 

類同您如何遍歷sortId,遍歷sortType

+0

當然啊,謝謝,我會接受答案,當它讓我。 –