* * * HELP!! My UPDATE Query Doesn't! * * *

Discussion in 'MySQL' started by MikeHayes, Jun 14, 2010.

  1. #1
    Calling all Gurus.

    The coding below does everything programmed to do with the exception of the UPDATE query. Sometimes it updates all rows but with the same array value.

    The purpose is to update all records in a table with one of 5 categories in order to test certain situations. That's the loop with $ix1.

    All combinations of quotes, single, double, - plus other variations - have not succeeded. An offer of a $25 reward on Sitepoint didn't help deliver a solution. The offer is still available

    I await with bated breath.

    And thanks in advance,

    Mike

    
    <?php
      include('misc.inc');
      $connection = mysql_connect($host,$user,$password)
            or die ('No connection');
    
      $db         = mysql_select_db($database,$connection)
            or die ('No selection');
    
      $query = "SELECT * FROM books ORDER BY booknumber";
      $result = mysql_query($query)
                or die('mysql error gevonden ' . mysql_error() . ' in query ' . $query);
    
    $ct1 = array('Kunst','Kinderbüchen','Literatur','Geography','Photography');
    $ix1 = -1;
    
      while ($row = mysql_fetch_array($result))
    
      {
         extract($row); 
    
         $ix1++;
         $cat = $ct1[$ix1];
    
    echo $cat,"<br>";
    
         $query   = "UPDATE books SET catalog='$ct1[$ix1]'";
    // and this version tried
    //   $query   = "UPDATE books SET catalog='$cat'";
         $result2 = mysql_query($query)
                or die('mysql error ' . mysql_error() . ' in query ' . $query);
    
    echo $booknumber,"<br>",$catalog,"<br>";
     
         if ($ix1 == 4)
            $ix1 = -1;
    };
    exit;
    ?>
    
    PHP:
    Table structuur.

    
      booknumber varchar(6) NOT NULL DEFAULT '1234'
      Author varchar(50) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT 'none'
      title varchar(30) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL DEFAULT 'tytel'
      keywords varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT ' '
      catalog varchar(25) DEFAULT NULL
      UNIQUE KEY `booknumber` (`booknumber`)
    
    PHP:
     
    MikeHayes, Jun 14, 2010 IP
  2. cDc

    cDc Peon

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    im not a php developer but should your query line be like this

    $query   = "UPDATE books SET catalog='" . $ct1[$ix1] + . "'";
    Code (markup):
     
    cDc, Jun 14, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    This wouldn't make much sense.

    I'm not clear on what you are trying to accomplish. Your current script would replace every value in the table with each loop. Is that what you're trying to accomplish?
     
    jestep, Jun 14, 2010 IP
  4. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    replace
    $query = "UPDATE books SET catalog='$ct1[$ix1]'";
    PHP:
    with
    $query = "UPDATE books SET catalog='$ct1[$ix1]' WHERE booknumber='$booknumber'";
    PHP:
     
    ignas2526, Jun 15, 2010 IP
  5. MikeHayes

    MikeHayes Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks guys for the identical suggestions. The result was:

    Mike
     
    MikeHayes, Jun 15, 2010 IP
  6. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That error has nothing to do with modification I posted, repost your current code...
     
    ignas2526, Jun 15, 2010 IP