如何使用pdo和多个数组更新mysql表?

时间:2016-08-01 13:11:18

标签: php mysql pdo

我在html表格中有一个表单字段列表。此页面旨在允许某人一次修改多个记录...使用更新查询。因为我有多个客户记录,我需要根据记录ID进行更新。我以为我应该创建一个复合数组,然后通过指向各种键,值对更新数据库,但似乎没有任何工作...所有的帮助都很感激..希望它的一个小错字我不是捕:

....SQL to pull data (works)...

echo "<form action='#' method='POST' >";
echo "<center><table>";
echo "<tr><th> Email Address </th> <th>Created On</th><th> Ip Address</th><th> Front of Card</th><th> Back of Card</th><th> Last 4 of Card</th><th> Decision</th><th> Notes (for Reject)</th>";
foreach ($customer as $row){


    echo '<tr><input type="hidden" name="record[]" value='.$row[' id '].'/>
    <td>'.$row["email"].' </td>
    <td>'.$row["created_on"].'</td>
    <td>'.$row["ip_address"].'</td>
    <td>
        <a href="imgReader.php?img='.$row["card_front"].'"><img class="fixed" src="imgReader.php?img='.$row[" card_front "].'"/></a>
    </td>
    <td>
        <a href="imgReader.php?img='.$row["card_back"].'"><img class="fixed" src="imgReader.php?img='.$row[" card_back "].'"/></a>
    </td>
    <td><input type="number" name="last4[]" value="" /></td>
    <td><select name="decision[]">
      <option value="PENDING">Pending</option>
      <option value="APPROVED">Approved</option>
      <option value="DENIED">Denied</option>
      <option value="DUPLICATE">Duplicate</option></select></td>
    <td><input type="textarea" rows="5" name="notes[]" value="" /></td>
</tr>';
if ($_POST){

$last4 = $_POST["last4"];
$status = $_POST["decision"];
$notes = $_POST["notes"];
$ids = $_POST["record"];


$tableSet = array('last4' => $last4,
      'decision' => $status,
      'notes' => $notes,
      'ids' => str_replace('/','', $ids)
      );
var_dump($tableSet);

foreach( $tableSet as $i) {


            $updateUserData = $db->prepare("UPDATE cards SET `last_4_cc` = :l4cc, `status` = :status, `notes` = :notes WHERE `id` = :record");

            $updateUserData->execute([
                ':l4cc' =>  $i[$tableSet["last4"]],
                ':status' => $i[$tableSet["decision"]],
                ':notes' => $i[$tableSet["notes"]],
                ':record' => $i[$tableSet["ids"]]
                ]);
    }

}
 ?>

0 个答案:

没有答案