strange behaviour using arrays to update records

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mantrid

    strange behaviour using arrays to update records

    Hello
    Im having problems working out why the following code does not work. I dont
    think its the sql as the error occurs on the first update which ever one is
    put there ($q1 or $q2). Ive swapped then around to test this.
    Help greatly appreciated

    The error is Unknown column 'A' in 'field list'
    but there is no field 'A'. im thinking that the 'A' may be first letter of
    the word Array, as if it is using this as a variable, but i dont know how.


    $commindexarray =$_POST['commindex'];
    $catarray=addsl ashes($_POST['category']);
    $commarray=adds lashes($_POST['comm']);
    $availarray=$_P OST['avail'];
    $delarray=$_POS T['del'];

    if($delarray==' '){
    for ($i = 0; $i < count($comminde xarray); $i++){
    $currtime=date( 'YmdHis');
    $q2 ="UPDATE usercomms SET typeid={$catarr ay[$i]} WHERE userid=
    ".$_SESSION['userid']." AND commindex ={$commindexarr ay[$i]}";//
    $updateusercomm = mysql_query($q2 ) or die('<br><span
    class=RedWarnin g>Sorry, there was a problem updating. Try
    again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
    $q="UPDATE comments SET typeid={$catarr ay[$i]}, comment='{$comm array[$i]}',
    available={$ava ilarray[$i]}, timestp='$currt ime', globalavail=0 WHERE
    commindex={$com mindexarray[$i]}";//
    $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
    there was a problem updating some records. Try again.<br><br>E rror - 2
    '.count($delarr ay).mysql_error ().'</span>');

    }
    }


  • Jerry Stuckle

    #2
    Re: strange behaviour using arrays to update records

    mantrid wrote:
    Hello
    Im having problems working out why the following code does not work. I dont
    think its the sql as the error occurs on the first update which ever one is
    put there ($q1 or $q2). Ive swapped then around to test this.
    Help greatly appreciated
    >
    The error is Unknown column 'A' in 'field list'
    but there is no field 'A'. im thinking that the 'A' may be first letter of
    the word Array, as if it is using this as a variable, but i dont know how.
    >
    >
    $commindexarray =$_POST['commindex'];
    $catarray=addsl ashes($_POST['category']);
    $commarray=adds lashes($_POST['comm']);
    $availarray=$_P OST['avail'];
    $delarray=$_POS T['del'];
    >
    if($delarray==' '){
    for ($i = 0; $i < count($comminde xarray); $i++){
    $currtime=date( 'YmdHis');
    $q2 ="UPDATE usercomms SET typeid={$catarr ay[$i]} WHERE userid=
    ".$_SESSION['userid']." AND commindex ={$commindexarr ay[$i]}";//
    $updateusercomm = mysql_query($q2 ) or die('<br><span
    class=RedWarnin g>Sorry, there was a problem updating. Try
    again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
    $q="UPDATE comments SET typeid={$catarr ay[$i]}, comment='{$comm array[$i]}',
    available={$ava ilarray[$i]}, timestp='$currt ime', globalavail=0 WHERE
    commindex={$com mindexarray[$i]}";//
    $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
    there was a problem updating some records. Try again.<br><br>E rror - 2
    '.count($delarr ay).mysql_error ().'</span>');
    >
    }
    }
    >
    >
    Non-numeric constants MUST be enclosed in single quotes in your SQL
    statements. Otherwise they may be taken as a column name.


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Miguel Cruz

      #3
      Re: strange behaviour using arrays to update records

      "mantrid" <ian.dandav@vir gin.netwrote:
      Im having problems working out why the following code does not work. I dont
      think its the sql as the error occurs on the first update which ever one is
      put there ($q1 or $q2). Ive swapped then around to test this.
      Help greatly appreciated
      >
      The error is Unknown column 'A' in 'field list'
      but there is no field 'A'. im thinking that the 'A' may be first letter of
      the word Array, as if it is using this as a variable, but i dont know how.
      >
      >
      $commindexarray =$_POST['commindex'];
      $catarray=addsl ashes($_POST['category']);
      $commarray=adds lashes($_POST['comm']);
      $availarray=$_P OST['avail'];
      $delarray=$_POS T['del'];
      >
      if($delarray==' '){
      for ($i = 0; $i < count($comminde xarray); $i++){
      $currtime=date( 'YmdHis');
      $q2 ="UPDATE usercomms SET typeid={$catarr ay[$i]} WHERE userid=
      ".$_SESSION['userid']." AND commindex ={$commindexarr ay[$i]}";//
      $updateusercomm = mysql_query($q2 ) or die('<br><span
      class=RedWarnin g>Sorry, there was a problem updating. Try
      again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
      $q="UPDATE comments SET typeid={$catarr ay[$i]}, comment='{$comm array[$i]}',
      available={$ava ilarray[$i]}, timestp='$currt ime', globalavail=0 WHERE
      commindex={$com mindexarray[$i]}";//
      $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
      there was a problem updating some records. Try again.<br><br>E rror - 2
      '.count($delarr ay).mysql_error ().'</span>');
      >
      }
      }
      PHP is not Perl. If you call addslashes() on an array, it treats it as a
      string, which means it has the value "Array". Then, when $i == 0 and you
      look at $catarray[$i], you get the first character, which is 'A'.

      You need to iterate through your arrays and call addslashes on each
      element individually. The most efficient way would be to move the
      addslashes (or perhaps mysql_real_esca pe_string instead) inside the $i
      for loop that you already have:

      for ($i = 0; $i < count($comminde xarray); $i++)
      {
      $cat = intval($_POST['category'][$i]);
      $comm = mysql_real_esca pe_string($_POS T['comm'][$i]);
      $q = "update usercomms
      set type = {$cat},
      comm = {$comm}
      where whatever";
      }

      miguel
      --
      Photos from 40 countries on 5 continents: http://travel.u.nu
      Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
      Airports of the world: http://airport.u.nu

      Comment

      • Norman Peelman

        #4
        Re: strange behaviour using arrays to update records

        "mantrid" <ian.dandav@vir gin.netwrote in message
        news:aNvBg.1255 38$sz1.84593@ne wsfe6-gui.ntli.net...
        Hello
        Im having problems working out why the following code does not work. I
        dont
        think its the sql as the error occurs on the first update which ever one
        is
        put there ($q1 or $q2). Ive swapped then around to test this.
        Help greatly appreciated
        >
        The error is Unknown column 'A' in 'field list'
        but there is no field 'A'. im thinking that the 'A' may be first letter of
        the word Array, as if it is using this as a variable, but i dont know how.
        >
        >
        $commindexarray =$_POST['commindex'];
        $catarray=addsl ashes($_POST['category']);
        $commarray=adds lashes($_POST['comm']);
        $availarray=$_P OST['avail'];
        $delarray=$_POS T['del'];
        >
        if($delarray==' '){
        for ($i = 0; $i < count($comminde xarray); $i++){
        $currtime=date( 'YmdHis');
        $q2 ="UPDATE usercomms SET typeid={$catarr ay[$i]} WHERE userid=
        ".$_SESSION['userid']." AND commindex ={$commindexarr ay[$i]}";//
        $updateusercomm = mysql_query($q2 ) or die('<br><span
        class=RedWarnin g>Sorry, there was a problem updating. Try
        again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
        $q="UPDATE comments SET typeid={$catarr ay[$i]},
        comment='{$comm array[$i]}',
        available={$ava ilarray[$i]}, timestp='$currt ime', globalavail=0 WHERE
        commindex={$com mindexarray[$i]}";//
        $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
        there was a problem updating some records. Try again.<br><br>E rror - 2
        '.count($delarr ay).mysql_error ().'</span>');
        >
        }
        }
        >
        >
        Not to mention that you are intermixing two styles of string
        concatination.. . try:

        $q2 ="UPDATE usercomms SET typeid={$catarr ay[$i]} WHERE
        userid={$_SESSI ON['userid']} AND commindex ={$commindexarr ay[$i]}";

        Norm
        --
        FREE Avatar hosting at www.easyavatar.com


        Comment

        • mantrid

          #5
          Re: strange behaviour using arrays to update records

          Ive made the changes you suggested. I dont get any errors now but the
          records are not updating. It is most puzzling. My updated code is below


          *************** *************** *************** *************** *************** *
          *****
          $delarray=$_POS T['del'];//not needed in the sql of first two queries so isnt
          in for loop

          if(count($delar ray)==0){
          for ($i = 0; $i < count($comminde xarray); $i++){
          $currtime=date( 'YmdHis');
          $cat = intval($_POST['category'][$i]);
          $comm = mysql_real_esca pe_string($_POS T['comm'][$i]);
          $avail =$_POST['avail'][$i];
          $commindex = intval($_POST['commindex'][$i]);

          $q2 ="UPDATE usercomms SET typeid=$cat WHERE userid=".$_SESS ION['userid']."
          AND commindex =$commindex";//
          $updateusercomm = mysql_query($q2 ) or die('<br><span
          class=RedWarnin g>Sorry, there was a problem updating. Try
          again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
          $q="UPDATE comments SET typeid=$cat, comment='$comm' , available=$avai l,
          timestp='$currt ime', globalavail=0 WHERE commindex=$comm index";//
          $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
          there was a problem updating some records. Try again.<br><br>E rror - 2
          '.count($delarr ay).mysql_error ().'</span>');
          }
          }

          *************** *************** *************** *************** ************

          Im also getting the following warning (below) on the bottom of the page. It
          isnt affecting anything as it also appears with other queries I run on this
          page which do work. Does anyone know what causes it. I dont think I can
          follow its suggestion for not displaying it as I dont think I can alter any
          settings on my hosting companies server. if that is what its asking me to
          do.



          Warning: Unknown(): Your script possibly relies on a session side-effect
          which existed until PHP 4.2.3. Please be advised that the session extension
          does not consider global variables as a source of data, unless
          register_global s is enabled. You can disable this functionality and this
          warning by setting session.bug_com pat_42 or session.bug_com pat_warn to off,
          respectively. in Unknown on line 0

          THANKS Ian


          "Miguel Cruz" <spam@admin.u.n uwrote in message
          news:spam-6605DF.14173407 082006@localhos t...
          "mantrid" <ian.dandav@vir gin.netwrote:
          Im having problems working out why the following code does not work. I
          dont
          think its the sql as the error occurs on the first update which ever one
          is
          put there ($q1 or $q2). Ive swapped then around to test this.
          Help greatly appreciated

          The error is Unknown column 'A' in 'field list'
          but there is no field 'A'. im thinking that the 'A' may be first letter
          of
          the word Array, as if it is using this as a variable, but i dont know
          how.


          $commindexarray =$_POST['commindex'];
          $catarray=addsl ashes($_POST['category']);
          $commarray=adds lashes($_POST['comm']);
          $availarray=$_P OST['avail'];
          $delarray=$_POS T['del'];

          if($delarray==' '){
          for ($i = 0; $i < count($comminde xarray); $i++){
          $currtime=date( 'YmdHis');
          $q2 ="UPDATE usercomms SET typeid={$catarr ay[$i]} WHERE userid=
          ".$_SESSION['userid']." AND commindex ={$commindexarr ay[$i]}";//
          $updateusercomm = mysql_query($q2 ) or die('<br><span
          class=RedWarnin g>Sorry, there was a problem updating. Try
          again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
          $q="UPDATE comments SET typeid={$catarr ay[$i]},
          comment='{$comm array[$i]}',
          available={$ava ilarray[$i]}, timestp='$currt ime', globalavail=0 WHERE
          commindex={$com mindexarray[$i]}";//
          $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
          there was a problem updating some records. Try again.<br><br>E rror - 2
          '.count($delarr ay).mysql_error ().'</span>');

          }
          }
          >
          PHP is not Perl. If you call addslashes() on an array, it treats it as a
          string, which means it has the value "Array". Then, when $i == 0 and you
          look at $catarray[$i], you get the first character, which is 'A'.
          >
          You need to iterate through your arrays and call addslashes on each
          element individually. The most efficient way would be to move the
          addslashes (or perhaps mysql_real_esca pe_string instead) inside the $i
          for loop that you already have:
          >
          for ($i = 0; $i < count($comminde xarray); $i++)
          {
          $cat = intval($_POST['category'][$i]);
          $comm = mysql_real_esca pe_string($_POS T['comm'][$i]);
          $q = "update usercomms
          set type = {$cat},
          comm = {$comm}
          where whatever";
          }
          >
          miguel
          --
          Photos from 40 countries on 5 continents: http://travel.u.nu
          Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
          Airports of the world: http://airport.u.nu

          Comment

          • Miguel Cruz

            #6
            Re: strange behaviour using arrays to update records

            "mantrid" <ian.dandav@vir gin.netwrote:
            Ive made the changes you suggested. I dont get any errors now but the
            records are not updating. It is most puzzling. My updated code is below
            >
            >
            *************** *************** *************** *************** *************** *
            *****
            $delarray=$_POS T['del'];//not needed in the sql of first two queries so isnt
            in for loop
            >
            if(count($delar ray)==0){
            for ($i = 0; $i < count($comminde xarray); $i++){
            $currtime=date( 'YmdHis');
            $cat = intval($_POST['category'][$i]);
            $comm = mysql_real_esca pe_string($_POS T['comm'][$i]);
            $avail =$_POST['avail'][$i];
            $commindex = intval($_POST['commindex'][$i]);
            >
            $q2 ="UPDATE usercomms SET typeid=$cat WHERE userid=".$_SESS ION['userid']."
            AND commindex =$commindex";//
            $updateusercomm = mysql_query($q2 ) or die('<br><span
            class=RedWarnin g>Sorry, there was a problem updating. Try
            again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
            $q="UPDATE comments SET typeid=$cat, comment='$comm' , available=$avai l,
            timestp='$currt ime', globalavail=0 WHERE commindex=$comm index";//
            $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
            there was a problem updating some records. Try again.<br><br>E rror - 2
            '.count($delarr ay).mysql_error ().'</span>');
            }
            }
            I would print out $q and $q2 to ensure that $_SESSION['userid'] contains
            what you thought it does.

            miguel
            --
            Photos from 40 countries on 5 continents: http://travel.u.nu
            Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
            Airports of the world: http://airport.u.nu

            Comment

            • mantrid

              #7
              Re: strange behaviour using arrays to update records


              *****
              $delarray=$_POS T['del'];//not needed in the sql of first two queries so
              isnt
              in for loop

              if(count($delar ray)==0){
              for ($i = 0; $i < count($comminde xarray); $i++){
              $currtime=date( 'YmdHis');
              $cat = intval($_POST['category'][$i]);
              $comm = mysql_real_esca pe_string($_POS T['comm'][$i]);
              $avail =$_POST['avail'][$i];
              $commindex = intval($_POST['commindex'][$i]);

              $q2 ="UPDATE usercomms SET typeid=$cat WHERE
              userid=".$_SESS ION['userid']."
              AND commindex =$commindex";//
              $updateusercomm = mysql_query($q2 ) or die('<br><span
              class=RedWarnin g>Sorry, there was a problem updating. Try
              again.<br><br>E rror - 1 '.count($delarr ay). mysql_error().' </span>');
              $q="UPDATE comments SET typeid=$cat, comment='$comm' , available=$avai l,
              timestp='$currt ime', globalavail=0 WHERE commindex=$comm index";//
              $updatecomm = mysql_query($q) or die('<br><span class=RedWarnin g>Sorry,
              there was a problem updating some records. Try again.<br><br>E rror - 2
              '.count($delarr ay).mysql_error ().'</span>');
              }
              }
              >
              I would print out $q and $q2 to ensure that $_SESSION['userid'] contains
              what you thought it does.
              >
              This I did and it is as expected,
              Another thing I tried was to put the queries outside of the loop and
              substitute the values from the arrays (which are also ok as i checked them
              when i ran phpinfo()) with numbers as shown below


              $q2 ="UPDATE usercomms SET typeid=1 WHERE userid=".$_SESS ION['userid']." AND
              commindex =339";
              $updateusercomm = mysql_query($q2 ) or die('<br><span class=RedWarnin g>Sorry,
              there was a problem updating. Try again.<br><br>E rror - 1
              '.count($delarr ay). mysql_error().' </span>');

              $q="UPDATE comments SET typeid=1, comment='ggdsff gdsfg', available=1,
              timestp='$currt ime', globalavail=0 WHERE commindex=339"; $updatecomm =
              mysql_query($q) or die('<br><span class=RedWarnin g>Sorry, there was a
              problem updating some records. Try again.<br><br>E rror - 2
              '.count($delarr ay).mysql_error ().'</span>');

              And this worked. so it looks like it is the loop or something

              by the way miguel I like your photos. i to love travelling but havent put
              mine on the web, something i'd like to do. much of mine are still on paper
              so a lot of work to do.

              Ian




              Comment

              • mantrid

                #8
                Re: strange behaviour using arrays to update records

                >
                This I did and it is as expected,
                Another thing I tried was to put the queries outside of the loop and
                substitute the values from the arrays (which are also ok as i checked them
                when i ran phpinfo()) with numbers as shown below
                >
                >
                $q2 ="UPDATE usercomms SET typeid=1 WHERE userid=".$_SESS ION['userid']."
                AND
                commindex =339";
                $updateusercomm = mysql_query($q2 ) or die('<br><span
                class=RedWarnin g>Sorry,
                there was a problem updating. Try again.<br><br>E rror - 1
                '.count($delarr ay). mysql_error().' </span>');
                >
                $q="UPDATE comments SET typeid=1, comment='ggdsff gdsfg', available=1,
                timestp='$currt ime', globalavail=0 WHERE commindex=339"; $updatecomm =
                mysql_query($q) or die('<br><span class=RedWarnin g>Sorry, there was a
                problem updating some records. Try again.<br><br>E rror - 2
                '.count($delarr ay).mysql_error ().'</span>');
                >
                And this worked. so it looks like it is the loop or something
                >
                by the way miguel I like your photos. i to love travelling but havent put
                mine on the web, something i'd like to do. much of mine are still on paper
                so a lot of work to do.
                >
                Ian
                >
                >

                ok getting closer. it is this bit

                for ($i = 0; $i < count($comminde xarray); $i++){

                i tested it with the following and it worked on those 100 records

                for ($i = 0; $i <= 100; $i += 1){


                ian


                Comment

                • mantrid

                  #9
                  Re: strange behaviour using arrays to update records

                  ok i got it
                  silly me
                  i hade commented out the setting of the variable

                  $commindexarray =$_POST['commindex'];


                  THANKS ALL for the help


                  "mantrid" <ian.dandav@vir gin.netwrote in message
                  news:gtJBg.5937 $oW3.5333@newsf e6-win.ntli.net...
                  >

                  This I did and it is as expected,
                  Another thing I tried was to put the queries outside of the loop and
                  substitute the values from the arrays (which are also ok as i checked
                  them
                  when i ran phpinfo()) with numbers as shown below


                  $q2 ="UPDATE usercomms SET typeid=1 WHERE userid=".$_SESS ION['userid']."
                  AND
                  commindex =339";
                  $updateusercomm = mysql_query($q2 ) or die('<br><span
                  class=RedWarnin g>Sorry,
                  there was a problem updating. Try again.<br><br>E rror - 1
                  '.count($delarr ay). mysql_error().' </span>');

                  $q="UPDATE comments SET typeid=1, comment='ggdsff gdsfg', available=1,
                  timestp='$currt ime', globalavail=0 WHERE commindex=339"; $updatecomm =
                  mysql_query($q) or die('<br><span class=RedWarnin g>Sorry, there was a
                  problem updating some records. Try again.<br><br>E rror - 2
                  '.count($delarr ay).mysql_error ().'</span>');

                  And this worked. so it looks like it is the loop or something

                  by the way miguel I like your photos. i to love travelling but havent
                  put
                  mine on the web, something i'd like to do. much of mine are still on
                  paper
                  so a lot of work to do.

                  Ian
                  >
                  >
                  ok getting closer. it is this bit
                  >
                  for ($i = 0; $i < count($comminde xarray); $i++){
                  >
                  i tested it with the following and it worked on those 100 records
                  >
                  for ($i = 0; $i <= 100; $i += 1){
                  >
                  >
                  ian
                  >
                  >

                  Comment

                  Working...