Jump to content

Update function with Array of Authors


billy_111

Recommended Posts

Hi,

 

I have the following code which basically checks an array of input textboxes and inserts them into a database table:

 

    public function insertAuthor($authArray, $PCorder=0)
    {
        $query = sprintf('SELECT Pid, Pname FROM People WHERE Pname IN(\'%s\') ORDER BY Pid ASC', implode('\',\'', $authArray));
        $result = mysql_query($query);

        $maxquery = "SELECT MAX(CPid) as max FROM ConfPaper WHERE CPRid = ".$_GET['CPRid'];
        $maxresult = mysql_query($maxquery);
        $max = mysql_fetch_array($maxresult);
        $CPid = $max['max'];

        if($result && mysql_num_rows($result) > 0)
        {
            $sqlValues = array();
            while(list($PId, $PName) = mysql_fetch_row($result))
            {
                if(in_array($PName, $authArray))
                {
                    $sqlValues[] = sprintf("(%d, 1, ".$CPid.", %d, now(), 0)", $PId
                                                               , $PCorder++ );
                    // Author already exists within the Pname table
                    // remove user from $authArray
                    $key = array_search($PName, $authArray);
                    unset($authArray[$key]);
                }
            }
        $sql  = "INSERT INTO PeopleCon(Person_id, PCHid, Pid, PCorder, PCdateadded, PCdeleted) VALUES \n";
        $sql .= implode(",\n", $sqlValues);
        $result = mysql_query($sql);
        }
        // If there are Authors left within the $authArray
        // Add them to the Pname table
        if(count($authArray) > 0)
        {
            People::insertPersons($authArray); // call insertPersons method for remaining authors
            $this->insertAuthor($authArray, $PCorder); // insert the remaining auhtors into PeopleCon
        }
    }

 

insertPersons method is as such:

 

    public function insertPersons($PName)
    {
        $query = "INSERT INTO People (Pname) VALUES ('" . implode("'), ('", $PName) . "')";
        $result = mysql_query($query);
    }

 

The logic works by firstly checking to see if an Author exists, if so they are inserted into PeopleCon, if not they are inserted into People AND PeopleCon. This all works perfectly..

 

 

But now what  i need to do is build an update method where users can edit the authors also. The logic would be more or less the same, but i wouldn't neeed an insertpersons method, i would need to change it, and also the script would edit the existing authors, and if anymore are added apply the same method as above.

 

Can anyone help me out as to how i should go about tackling this?

 

Thanks

 

Kind regards

Billy

Link to comment
Share on other sites

Ok, the method's logic will run so that it checks to see if an author from table exists,  if so add the results to a table called PeopleCon, else insert in to Poeple. From looking at my code, do you see any problems? How can i do this update statement..

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.