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

4 replies
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 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 Code:
<?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;
?>
Table structuur.

PHP Code:
  booknumber varchar(6NOT NULL DEFAULT '1234'
  
Author varchar(50CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT 'none'
  
title varchar(30CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL DEFAULT 'tytel'
  
keywords varchar(100CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT ' '
  
catalog varchar(25) DEFAULT NULL
  UNIQUE KEY 
`booknumber` (`booknumber`) 
#update
  • Profile picture of the author Manfred Ekblad
    Originally Posted by MikeHayes View Post

    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.
    Yes, that is the effect of doing an UPDATE query without a WHERE-clause. It will update all rows with the same value, since there is no condition specified.

    Originally Posted by MikeHayes View Post

    The purpose is to update all records in a table with 5 categories in order to test certain situations. That's the loop with .
    The code SELECTs all the rows from table 'books'. Then it loops through all the rows in that table.

    For each iteration it runs an UPDATE query which sets the catalog to the value in $ct1[$ix1] for all rows in the table.

    So, the expected result would be that after a run, all the rows have the same catalog value. I've checked, and they do...

    Originally Posted by MikeHayes View Post

    All combinations of quotes, single, double, - plus other variations - have not succeeded.
    Can you provide more test data? I don't have any issues with updates, it always updates.

    How about the echo statements... first you echo the new value in $cat, then after the update you echo $booknumber and $catalog which are the old values.

    The code doesn't really do what you want it to do, if you just want to test the different catalog values with all the rows... hang on, I'm writing some code for you
    {{ DiscussionBoard.errors[2216982].message }}
  • Profile picture of the author Manfred Ekblad
    <?php

    include('misc.inc');

    $connection = mysql_connect($host,$user,$password) or die ('No connection');
    $db = mysql_select_db($database,$connection) or die ('No selection');

    $ct1 = array('Kunst','Kinderbüchen','Literatur','Geograp hy','Photography');

    echo "<h2>Update all the rows at once</h2>";
    foreach($ct1 as $cat)
    {

    echo "<h3>New catalog: " . $cat . "</h3>";
    echo "<p>";

    $query = "UPDATE books SET catalog='$cat'";
    $result = mysql_query($query) or die('mysql error ' . mysql_error() . ' in query ' . $query);

    echo "<strong>All rows updated.</strong>";
    echo "</p>";

    }

    echo "<h2>Update the rows one by one</h2>";
    foreach($ct1 as $cat)
    {

    echo "<h3>New catalog: " . $cat . "</h3>";
    echo "<p>";

    $query = "SELECT * FROM books ORDER BY booknumber";
    $result = mysql_query($query) or die('mysql error gevonden ' . mysql_error() . ' in query ' . $query);

    while ($row = mysql_fetch_array($result))
    {
    $booknumber = $row["booknumber"];
    $catalog = $row["catalog"];
    $query2 = "UPDATE books SET catalog='$cat' WHERE booknumber='$booknumber'";
    mysql_query($query2) or die('mysql error ' . mysql_error() . ' in query ' . $query2);
    echo "Booknumber: " . $booknumber . "<br/>";
    echo "Old catalog: " . $catalog . "<br/>";
    }

    echo "<strong>All rows updated.</strong>";
    echo "</p>";

    }

    exit;

    ?>
    {{ DiscussionBoard.errors[2217043].message }}
  • Profile picture of the author MikeHayes
    Hi Manfred,

    Thanks for your contribution. It taught me some new things. But your code didn't achieve what I wanted. At the end all rows had the same category, the last one - Photography.
    Where I want to be is as follows:
    Booknumber: 1
    Old catalog: Kunst
    Booknumber: 2
    Old catalog: Kinderbüchen
    Booknumber: 4
    Old catalog: Literatur
    Booknumber: 5
    Old catalog: Geography
    Booknumber: 1234
    Old catalog: Photography
    Booknumber: 123456
    Old catalog: Kunst
    Booknumber: 999999
    Old catalog: Kinderbüchen

    Regards,

    Mike
    Signature

    * * * * My site * * * *
    * * * * My paintings * * * *

    {{ DiscussionBoard.errors[2221040].message }}
  • Profile picture of the author Manfred Ekblad
    Originally Posted by MikeHayes View Post

    The purpose is to update all records in a table with 5 categories in order to test certain situations. That's the loop with .
    Yeah, I guess it's this part that made me confused then haha

    You got it working already or you still need some code?
    {{ DiscussionBoard.errors[2221361].message }}

Trending Topics