Jump to content

Archived

This topic is now archived and is closed to further replies.

Jordi_E

Function seems executed twice when called only once.

Recommended Posts

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:

[color=green]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;
    }
}[/color]

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.


Share this post


Link to post
Share on other sites
Try This

[code]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;
    }
}[/code]

That should make it stop doing that.

Share this post


Link to post
Share on other sites
[quote author=businessman332211@hotmail.com link=topic=100916.msg399230#msg399230 date=1153237012]
Try This

[code]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;
    }
}[/code]

That should make it stop doing that.
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

[code]

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;
    }
}
[/code]

Share this post


Link to post
Share on other sites
Change this:
[code]return ($this->num_rows() == 1); [/code]
to this:
[code]return (($this->num_rows() == 1) ? true : false);[/code]
The change this:
[code]if (!$this->BOX_exists( $BOX_id ))[/code]
to this:
[code]if ($this->BOX_exists( $BOX_id ) === true)[/code]

Share this post


Link to post
Share on other sites
[quote author=redarrow link=topic=100916.msg399285#msg399285 date=1153242888]
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.

[code]

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;
    }
}
[/code]
[/quote]

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

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=100916.msg399309#msg399309 date=1153244586]
Change this:
[code]return ($this->num_rows() == 1); [/code]
to this:
[code]return (($this->num_rows() == 1) ? true : false);[/code]
The change this:
[code]if (!$this->BOX_exists( $BOX_id ))[/code]
to this:
[code]if ($this->BOX_exists( $BOX_id ) === true)[/code]

[/quote]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.