SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Gilroy, CA
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if(mysql_query("insert bad data") doesn't return false?

    Hi.

    I put an insert statement inside an if statement to test if the insert is succesful :

    foreach($print as $key => $value){
    if($qty[$key] > 0){
    if($rs=mysql_query("insert into inventory (location,invqty,invcid,invprint) values ('$location',$qty[$key],$cid,'$print[$key]')")){
    echo "inserted $qty[$key] pieces of $print[$key] into $location<br>";
    } else {
    echo "<br>insert of $print[$key] for $qty[$key] pieces failed<br>";
    }
    }
    }

    However, if I put bad data in the insert, e.g. a non-numeric qty in a an integer field, it doesn't execute the else portion of the if statement. No error, either.

    PHP docs say:
    Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of SQL statements, mysql_query() returns TRUE on success and FALSE on error.

    What's up with this?

    Thanks for the help,
    Bernn

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should work
    PHP Code:
    foreach($print as $key => $value){ 
      if(
    $qty[$key] > 0) {
        
    $rs mysql_query("INSERT INTO inventory (location, invqty, invcid, invprint) VALUES ('$location', $qty[$key]$cid, '$print[$key]')");
        if(
    $rs == TRUE) {
          echo 
    "inserted " .$qty[$key]. " pieces of " .$print[$key]. " into " .$location"<br>";
        } else {
          echo 
    "<br>insert of " .$print[$key]. " for " .$qty[$key]. " pieces failed<br>";
          echo 
    mysql_errno($rs). ": " .mysql_error($rs). "\n";
        }
      }

    Last edited by notepad_coder; Jun 20, 2002 at 19:58.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Gilroy, CA
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nope, didn't work

    I was going to ask how that was any different, but it didn't work either.

    Thanks for the suggestion, though.

    Any other ideas?
    Thanks
    Bernn

  4. #4
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should have posted the errors if it didnt work.
    Try using this to see if it's an INT field where qty[$key] is
    PHP Code:
    foreach($print as $key => $value){ 
      if(
    $qty[$key] > 0) {
        
    $rs mysql_query("INSERT INTO inventory (location, invqty, invcid, invprint) VALUES ('$location', $qty[$key]$cid, '$print[$key]')") or die(mysql_errno(). ": " .mysql_error());
      }

    if you get an error try using '$qty[$key]'.
    Last edited by notepad_coder; Jun 20, 2002 at 20:33.

  5. #5
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bernn,

    When you say the code notepad_coder posted doens't work, what is wrong with it?

    Does it give you an error?

    Or does it not work as you expect? In which case, what happens?

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Gilroy, CA
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nothing Happens

    The page loads without an error. It doesn't execute the echo statement inside the if or the else sections.
    Nothing happens.
    Of course, no insert occurs in mysql either.

  7. #7
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is $qty? That cold be the problem. Can you post more of the code so we can see if there is a problem in your array?

  8. #8
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with notepad_coder. If an if statment with an else clause is executed without an error, either the code in the if or else section is going to be executed.

    The only possiblity is the if statment is never being executed.

    You could try print_r()ing $qty to see if it has the correct values in it.

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Gilroy, CA
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Oops a non-numeric value is testing as !>0

    The first conditional is weeding out non-numeric values:
    if($qty[$key] > 0) {

    I just assumed that a non-numeric value would pass the >0 test but it doesn't. The insert statement was never executed.

    Sorry for the trouble. Thanks for the help.

    Bernn

  10. #10
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The is_numeric() function would do the trick. And if you want to check if it is not numeric, you can do !is_numeric().


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
  •