I need to update a mysql record where a item is checked marked.. Its the equal part I am having a issue with. $sqlupdate = $dbh->prepare("UPDATE `UpdatedeadAucflys` SET Newfly=:id1 WHERE bugID=:aucnum AND 1=verride"); I was using 1 for when its checked.. but when its looped it updates the wrong row... Any pointers would be appreciated. <?php if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "save")) { try { $sqlupdate = $dbh->prepare("UPDATE `UpdatedeadAucflys` SET Newfly=:id1 WHERE bugID=:aucnum AND 1=:override"); $sqlupdate->bindParam(':id1',$sendfly2, PDO::PARAM_STR); $sqlupdate->bindParam(":aucnum", $id, PDO::PARAM_INT); $sqlupdate->bindParam(":override", $check, PDO::PARAM_INT); foreach ($_POST['fly'] as $index => $sendfly2){ $id = $_POST['bedid'][$index]; $check = $_POST['override'][$index]; $sqlupdate->execute(); } } catch(Exception $e) { echo '<font color="red">An error has occured: ' . $e->getMessage() . '</font>'; exit(); } $insertGoTo = "updated.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } ?> <form action="<?php echo $editFormAction; ?>" id="save" name="save" method="POST"/> <?php $updatesmade= $dblp->query("SELECT bugID,Currentfly,Newfly,Partbigbird FROM `UpdatedeadAucflys` WHERE bugID='$aucupdateid'"); $sendnow->setFetchMode(PDO::FETCH_ASSOC); while($showupdates = $updatesmade->fetch()) { echo '<input type="text" name="fly[]" value="'.$fly.'"><input type="checkbox" id="myCheck'.$equalssame.'" name="override[]" value="'.$aucupdateid.'"> - '.$showupdates['Newfly']; } ?> <BR> <input type="hidden" name="bedid[]" value="<?php echo $showrows['bedID'];?>"> <?php ?> <input type="hidden" name="MM_update" value="save"> ] </form> Code (markup):
Maybe try this? <?php if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "save")) { try { $sqlupdate = $dbh->prepare("UPDATE `UpdatedeadAucflys` SET Newfly=:id1 WHERE bugID=:aucnum AND 1=:override"); //Wonder if this would work with simply writing AND :override foreach ($_POST['fly'] as $index => $sendfly2){ $id = $_POST['bedid'][$index]; if (isset($_POST['override'][$index]) && $_POST['override'][$index] == 1) { $check = 1; } else { $check = 0; } $sqlupdate->bindParam(':id1',$sendfly2, PDO::PARAM_STR); $sqlupdate->bindParam(":aucnum", $id, PDO::PARAM_INT); $sqlupdate->bindParam(":override", $check, PDO::PARAM_INT); $sqlupdate->execute(); } } PHP:
Still no go... For example if I have 5 records and I checkmark the first record and last record then click update it only updates the first two then...
with your notes I can use just verride but it still doesn't update the correct row.. I am not sure what I am missing here.. My checkmark value is <input type="checkbox" name="override[]" value="1">
Hmm, maybe it has to do with your foreach loop? Is it updating anything? What is it updating or not updating? Any errors? <?php $i=0; while ($i < count($_POST['fly'])){ $sendfly2 = $_POST['fly'][$i]; $id = $_POST['bedid'][$i]; $i++ PHP: if it still does not work perhaps you could look at the post array structure by doing this <?php print_r($_POST); PHP:
With the code you gave me before it. It does update. So in other words you have 5 records for example and I want to update the the ones with the x. ( The x is whats checked marked) I click \/ record1 x record2 record3 x record4 x record5 My results record1 x record2 x record3 x record4 record5 So if I select 1 anywhere. It will update the first record. If I select 2 anywhere at the bottom or middle it will update the first 2 records. If I select 3 anywhere it will update the first 3 records... etc.
Ah ok, that is because the checkbox that is not checked will not be a value in the array. So there wont be an array item for record 2 and 5. Stupid of me not to see that. So change it up, you need to set the value to the account number or whatever the id is <?php $i=0; while($showupdates = $updatesmade->fetch()) { echo '<input type="text" name="fly[]" value="'.$fly.'"><input type="hidden" name="update[]" value="'.$aucupdateid.'"><input type="checkbox" id="myCheck'.$equalssame.'" name="id[]" value="'.$i.'"> - '.$showupdates['Newfly']; $i++; } PHP: <?php foreach ($_POST['id'] as $id){ $sendfly = $_POST['fly'][$id]; $aucnum = $_POST['update'][$id]; $sqlupdate->bindParam(':id1',$sendfly, PDO::PARAM_STR); $sqlupdate->bindParam(":aucnum", $aucnum, PDO::PARAM_INT); $sqlupdate->execute(); } PHP: <?php $sqlupdate = $dbh->prepare("UPDATE `UpdatedeadAucflys` SET Newfly=:id1 WHERE bugID=:aucnum"); PHP:
You are the man!!! I did try my loops but I had something incorrect beofre Also I didn't use the loop inside my checkbox . Thank you so much!!!