php pdo update where item is check marked

Discussion in 'PHP' started by xbat, Oct 21, 2014.

  1. #1
    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=:eek: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):
     
    Solved! View solution.
    xbat, Oct 21, 2014 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    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:
     
    Anveto, Oct 21, 2014 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3

    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...
     
    xbat, Oct 22, 2014 IP
  4. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #4
    with your notes I can use just :eek: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">
     
    xbat, Oct 22, 2014 IP
  5. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #5
    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:
     
    Anveto, Oct 22, 2014 IP
  6. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #6
    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.
     
    xbat, Oct 22, 2014 IP
  7. #7
    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:
     
    Anveto, Oct 22, 2014 IP
  8. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #8
    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!!! :)
     
    xbat, Oct 23, 2014 IP