Jump to content

How can i do this method..


billy_111

Recommended Posts

Hi,

 

I have a problem and can't seem to figure out how to do it..

 

Basically i have these 2 tables:

 

People

Pid

Pname

Pdateadded

Pdeleted

 

And..

 

Quote:

PeopleCon

PCid

Pid

PCorder

Pdateadded

Pdeleted

 

Now, currently i insert the data into a table "People".. like you guys have helped me with..

 

But what i need to do is, first check to see if the author already exists in the People table, if so then just INSERT into PeopleCon, ELSE INSERT into both People and PeopleCon...

 

Also if there are 4 authors, and 2 exist in People, and 2 do not. What needs to happen is the first 2 are simply inserted into PeopleCon, but the last 2 are INSERTED into both People and PeopleCon. When the authors are inserted into PeopleCon, the PCorder row needs to increment by the number of authors byt order of author.

 

So this is the logic:

 

[*]Multiple authors are entered in textboxes

[*]Fire the script

[*]Check to see if author already exists in "People" table

[*]IF it does exist, INSERT into PeopleCon table

[*]ELSE do the insert into the People AND PeopleCon table, first it will INSERT into the People, and GET the MAX (ID) and insert into the PeopleCon

 

So i currently have this, but as 3 separate methods:

 



class People {

    public function checkNameExists(){

      $query = "SELECT * FROM People WHERE Pname = '". mysql_real_escape_string($_POST['author'])."'";

        $result = mysql_query($query);
        if(mysql_num_rows($result) > 0):
            $row = mysql_fetch_array($result);
            return true;
        else:
            return false;
        endif;
    }

    public function insertAuthor(){
        $callback = create_function('$author','return "(\'".mysql_real_escape_string($author)."\',NOW(),0)";');
        $sql = sprintf(
            'INSERT INTO People (Pname, Pdateadded, Pdeleted) VALUES %s'
            ,implode(',',array_map($callback,$_POST['author']))
        );
        $result = mysql_query($sql);
        return "Successfully added NEW author";
    }

    public function insertAuthorCon(){

        $sql = "INSERT INTO PeopleCon
                    (Pid, PCorder, PCdateadded, PCdeleted) VALUES
                    (
                    'MAX ID WILL GO HERE',
                    'INCREMENT OF ORDER GOES HERE',
                    now(),
                    0
                    )";
        $result = mysql_query($sql);
        return "Successfully added existing author";
    }

 

But obviously i need to combine these together into one method to match what i am trying to achieve..

 

Can anyone point me in the right direction?

 

Thanks again...

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.