Jump to content


Photo

Function seems executed twice when called only once.


  • Please log in to reply
6 replies to this topic

#1 Jordi_E

Jordi_E
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 July 2006 - 06:45 PM

Hello everybody.

I'm newbie in PHP.

I am running PHP version 5.1.2 together with Apache version 2.0.52 and mySQL version 4.1 on a Windows XP system.

Consider these functions:

function BOX_exists( $BOX_id )
{
    $sql = "SELECT IDBOX FROM box ".
          "WHERE IDBOX='".$BOX_id."';";

    $this->query($sql);
    return ($this->num_rows() == 1);
}


function insert_BOX( $BOX_id, $Description)
{
    if (!$this->BOX_exists( $BOX_id ))
    {
        $sql =  "INSERT INTO box (IDBOX, Description )".
                "VALUES ('$BOX_id', '$Description');";

        $this->query( $sql );
        return true;
    }
    else
    {
        return false;
    }
}


The problem I have is that calling to insert_BOX function, always inserts correctly data on mySQL, but the return value from insert_BOX is sometimes false (not always, sometimes is true). As can be seen, this should be impossible, since data is correctly inserted.

I only call insert_BOX once, but what would happen in case insert_BOX is executed twice for some reason? The second call would return false. I feel this is the problem I'm having.

I don't know how to handle this problem. I spent many hours without success.
I will appreciate any idea or help.
Thanks in advance.




#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 18 July 2006 - 03:36 PM

Try This

function BOX_exists( $BOX_id ) 
{
    $sql = "SELECT IDBOX FROM box ".
           "WHERE IDBOX='".$BOX_id."';"; 

    $this->query($sql);
    return ($this->num_rows() == 1); 
}


function insert_BOX( $BOX_id, $Description) 
{
    if (BOX_exists( $BOX_id ))
    {
        $sql =  "INSERT INTO box (IDBOX, Description )".
                "VALUES ('$BOX_id', '$Description');";

        $this->query( $sql ); 
        return true; 
    }
    else
    {
        return false;
    }
}

That should make it stop doing that.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 Jordi_E

Jordi_E
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 July 2006 - 05:04 PM

Try This

function BOX_exists( $BOX_id ) 
{
    $sql = "SELECT IDBOX FROM box ".
           "WHERE IDBOX='".$BOX_id."';"; 

    $this->query($sql);
    return ($this->num_rows() == 1); 
}


function insert_BOX( $BOX_id, $Description) 
{
    if (BOX_exists( $BOX_id ))
    {
        $sql =  "INSERT INTO box (IDBOX, Description )".
                "VALUES ('$BOX_id', '$Description');";

        $this->query( $sql ); 
        return true; 
    }
    else
    {
        return false;
    }
}

That should make it stop doing that.


Thanks a lot for your answer.
But obviously I use  if (!BOX_exists( $BOX_id ))  prior inserting a new BOX. If it exists in the Database, I wish not to insert it.
So, changing from (!BOX_exists( $BOX_id )) to (BOX_exists( $BOX_id )) is not the solution.
Best regards.


#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 18 July 2006 - 05:14 PM

try it this way but only chaged this  ($this->num_rows() > 0);

ps. i had a go my self got the same result two inserts dont no why theo.

i read the code and basickly its saying if theres one insert but the code says if there 1 insert with another one.

can not work it out theo sorry.

ps i do get the inpression becouse your pulling one function into another that why there a double entry.


function BOX_exists( $BOX_id )
{
    $sql = "SELECT IDBOX FROM box ".
           "WHERE IDBOX='".$BOX_id."';";

    $this->query($sql);
    return ($this->num_rows() > 0);
}


function insert_BOX( $BOX_id, $Description)
{
    if (!$this->BOX_exists( $BOX_id ))
    {
        $sql =  "INSERT INTO box (IDBOX, Description )".
                "VALUES ('$BOX_id', '$Description');";

        $this->query( $sql );
        return true;
    }
    else
    {
        return false;
    }
}


try this ok.


function insert_BOX( $BOX_id, $Description) 
{
  

    $sql = "SELECT IDBOX FROM box ".
           "WHERE IDBOX='".$BOX_id."';"; 

    $this->query($sql);
    return ($this->num_rows() > 0); 




  if (!$this->BOX_exists( $BOX_id ))
    {

 $sql =  "INSERT INTO box (IDBOX, Description )".
                "VALUES ('$BOX_id', '$Description');";

        $this->query( $sql ); 
        return true; 
    }
    else
    {
        return false;
    }
} 

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 18 July 2006 - 05:43 PM

Change this:
return ($this->num_rows() == 1);
to this:
return (($this->num_rows() == 1) ? true : false);
The change this:
if (!$this->BOX_exists( $BOX_id ))
to this:
if ($this->BOX_exists( $BOX_id ) === true)


#6 Jordi_E

Jordi_E
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 July 2006 - 06:24 PM

try it this way but only chaged this  ($this->num_rows() > 0);

ps. i had a go my self got the same result two inserts dont no why theo.

i read the code and basickly its saying if theres one insert but the code says if there 1 insert with another one.

can not work it out theo sorry.

ps i do get the inpression becouse your pulling one function into another that why there a double entry.


function BOX_exists( $BOX_id )
{
    $sql = "SELECT IDBOX FROM box ".
           "WHERE IDBOX='".$BOX_id."';";

    $this->query($sql);
    return ($this->num_rows() > 0);
}


function insert_BOX( $BOX_id, $Description)
{
    if (!$this->BOX_exists( $BOX_id ))
    {
        $sql =  "INSERT INTO box (IDBOX, Description )".
                "VALUES ('$BOX_id', '$Description');";

        $this->query( $sql );
        return true;
    }
    else
    {
        return false;
    }
}


try this ok.


function insert_BOX( $BOX_id, $Description) 
{
  

    $sql = "SELECT IDBOX FROM box ".
           "WHERE IDBOX='".$BOX_id."';"; 

    $this->query($sql);
    return ($this->num_rows() > 0); 




  if (!$this->BOX_exists( $BOX_id ))
    {

 $sql =  "INSERT INTO box (IDBOX, Description )".
                "VALUES ('$BOX_id', '$Description');";

        $this->query( $sql ); 
        return true; 
    }
    else
    {
        return false;
    }
} 


Thanks for your answer. I tried what you suggest, but it still behaves bad.

#7 Jordi_E

Jordi_E
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 July 2006 - 06:44 PM

Change this:

return ($this->num_rows() == 1);
to this:
return (($this->num_rows() == 1) ? true : false);
The change this:
if (!$this->BOX_exists( $BOX_id ))
to this:
if ($this->BOX_exists( $BOX_id ) === true)


Thanks a lot for your ideas.
I tried it, but I still have the same problem.

On my explanation I forgot to mention that these functions are inside of a class which inherits from another class called DB, which is implementing the query and the Database connection




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users