PHP/MySQL UPDATE problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matthud@gmail.com

    PHP/MySQL UPDATE problem

    //GET INSERTED CHUNK ID
    $getid = mysql_query("SE LECT id FROM chunks WHERE chunk = '$chunk'");
    $idrow = mysql_fetch_arr ay($getid, MYSQL_ASSOC);
    $chunkid = $idrow["id"].",";


    $eachtag = explode(",", $tags);
    foreach($eachta g as $key=>$value) {
    For some reason, this code:

    $updated = $row["chunks"] . $chunkid;
    mysql_query("UP DATE tagtochunk SET chunks = '$updated' WHERE tag =
    '$value'");

    only updates the field with $chunkid, leaving out row["chunks"]. I'm
    guessing it may have something to do with scope within control
    structures.... I'm at a loss.



    $query = "SELECT chunks FROM tagtochunk WHERE tag = '$value'";
    $result = mysql_query($qu ery);

    if (mysql_num_rows ($result) == 0) {

    //CREATE NEW TAG ENTRY
    mysql_query("IN SERT INTO tagtochunk (tag, chunks) VALUES ('$value',
    '$chunkid')");

    } else {

    //UPDATE CUR TAG WITH NEW CHUNKID
    $updated = $row["chunks"].$chunkid;
    mysql_query("UP DATE tagtochunk SET chunks = '$updated' WHERE tag =
    '$value'");

    }

    }

    mysql_close($co n);
    ?>

  • Kimmo Laine

    #2
    Re: PHP/MySQL UPDATE problem

    matthud@gmail.c om kirjoitti:
    //GET INSERTED CHUNK ID
    $getid = mysql_query("SE LECT id FROM chunks WHERE chunk = '$chunk'");
    $idrow = mysql_fetch_arr ay($getid, MYSQL_ASSOC);
    $chunkid = $idrow["id"].",";
    >
    >
    $eachtag = explode(",", $tags);
    foreach($eachta g as $key=>$value) {
    For some reason, this code:
    >
    $updated = $row["chunks"] . $chunkid;
    Where does $row come from? Earlier in the code you use $idrow. Try
    echoing the value of $updated and print_r($row);

    --
    "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
    spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)

    Comment

    • matthud@gmail.com

      #3
      Re: PHP/MySQL UPDATE problem

      That was a good catch, unfortunately it didn't do the trick.
      Here the code is updated with $row.

      $getid = mysql_query("SE LECT id FROM chunks WHERE chunk = '$chunk'");
      $idrow = mysql_fetch_arr ay($getid, MYSQL_ASSOC);
      $chunkid = $idrow['id'] . ",";


      $eachtag = explode(",", $tags);
      foreach($eachta g as $key=>$value) {

      $query = "SELECT tag FROM tagtochunk WHERE tag = '$value'";
      $result = mysql_query($qu ery);

      if (mysql_num_rows ($result) == 0) {

      //CREATE NEW TAG ENTRY
      mysql_query("IN SERT INTO tagtochunk VALUES ('$value', '$chunkid')");

      } else {

      //UPDATE CUR TAG WITH NEW CHUNKID
      $query = "SELECT tag FROM tagtochunk WHERE tag = '$value'";
      $row = mysql_fetch_arr ay($query, MYSQL_ASSOC);
      $updated = $row["chunks"] . $chunkid;
      mysql_query("UP DATE tagtochunk SET chunks = '$updated' WHERE tag =
      '$value'");

      }

      }

      Comment

      • matthud@gmail.com

        #4
        Re: PHP/MySQL UPDATE problem

        That was a good catch, unfortunately it didn't do the trick.
        Here the code is updated with $row.

        //GET INSERTED CHUNK ID
        $getid = mysql_query("SE LECT id FROM chunks WHERE chunk = '$chunk'");
        $idrow = mysql_fetch_arr ay($getid, MYSQL_ASSOC);
        $chunkid = $idrow["id"].",";


        $eachtag = explode(",", $tags);
        foreach($eachta g as $key=>$value) {

        $query = "SELECT chunks FROM tagtochunk WHERE tag = '$value'";
        $result = mysql_query($qu ery);

        if (mysql_num_rows ($result) == 0) {

        //CREATE NEW TAG ENTRY
        mysql_query("IN SERT INTO tagtochunk (tag, chunks) VALUES ('$value',
        '$chunkid')");

        } else {

        //UPDATE CUR TAG WITH NEW CHUNKID
        $row = mysql_fetch_arr ay($query, MYSQL_ASSOC);
        $updated = $row["chunks"].$chunkid;
        mysql_query("UP DATE tagtochunk SET chunks = '$updated' WHERE tag =
        '$value'");

        }

        }


        Comment

        • Kimmo Laine

          #5
          Re: PHP/MySQL UPDATE problem

          matthud@gmail.c om kirjoitti:
          $query = "SELECT chunks FROM tagtochunk WHERE tag = '$value'";
          ....
          $row = mysql_fetch_arr ay($query, MYSQL_ASSOC);
          I've done this mistake many times. See, $query is the actual query
          string, you need $result for mysql_fetch_arr ay() :)

          --
          "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
          spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)

          Comment

          Working...