Jump to content

Function seems executed twice when called only once.


Jordi_E

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.


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.