简体   繁体   中英

Update array PDO

I was working with this code:

$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();

It works as it should so I proceeded to add extra values

$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

 <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>

I had assumed this would be fine as it's only adding onto something that already works however it's not quite as simple as I originally thought it would be, currently I'm getting the error message Notice: Array to string conversion I know it's relating to the additional code I added but I'm not sure what I'm doing wrong.

having sortType[] in your form makes your $sortType an array.

you bind the array here:

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

what you need to bind is more like

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

And then add to foreach

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

similiar to how you iterate through sortId, iterate through sortType

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM