Results 1 to 6 of 6

Thread: INSERT INTO Query

  1. #1
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default INSERT INTO Query

    Struggling with something.

    Have an INSERT INTO statement for my base but I'm trying to add a WHERE clause to it and it's not happening for me. I think because I'm trying to use NOT LIKE and I'm not sure how to use it. ie.

    Code:
    'INSERT INTO database WHERE name NOT LIKE "blue"';
    Do I need to use %?

    Please help!

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I was always under the impression that the WHERE clause does not work with the INSERT clause. The MySQL Dev site does not say anything about it either.

    http://dev.mysql.com/doc/refman/5.1/en/insert.html

    What exactly are you trying to accomplish?
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sure, the query basically is:

    Code:
    foreach ($xml->xpath('//prod') as $character) {
    
      $name = $character["name"];
    
    mysql_query("
    INSERT INTO xml (
    name) 
    VALUES (
    '$name') 
    WHERE name NOT LIKE 'Tom'") or die(mysql_error());
    
    }
    In other words using a foreach statement, run through a list of names and only "insert" ones that aren't like Tom.

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I think your best bet would be to use an if conditional statement to check if $name or $character['name'] is not equal to the name "Tom"( if (strtolower($name) != 'tom') ) then execute the insert statement in the mysql_query. Let me know if you need to me clarify this more.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I think you need to use UPDATE instead of INSERT. This is a name column that already holds a "name" value, is it not (I assume so, since we're checking against it)?
    [not tested]
    PHP Code:
    <?php
    foreach ($xml->xpath('//prod') as $character) {

      
    $name $character["name"];

      
    mysql_query("UPDATE xml SET name = '$name' WHERE name NOT LIKE 'Tom'") or die(mysql_error());

    }
    however, I suspect this won't do what you expect. (I don't think your original code would, either, if it did work.) It would loop through all the new names, but each UPDATE would be performed on the entire table. So, you'd end up with everyone's name being set to the name of the last character to go through the foreach loop (except for the characters named Tom, of course).

    Try incorporating thetestingsite's suggestion - compare the existing name first, then make a more specific replacement:
    [not tested]
    PHP Code:
    <?php
    foreach ($xml->xpath('//prod') as $character) {

      if(
    strtolower($character['name']) != 'tom'){
        
    $name $character["name"];
        
    mysql_query("UPDATE xml SET name = '$name' WHERE id = '$character['id']'") or die(mysql_error());
      }
    }
    Last edited by traq; 09-20-2009 at 06:24 AM.

  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    I don't know why, but it's a really common problem for people to use WHERE in INSERT clauses [this is incorrect of course].
    - Josh

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •