Function to insert a tag into a database I'm developing a MVC structured RESTful API project (thanks to the help of Mārtiņš Tereško for explaining it).

Almost all of my functions look similar to this:

 /**
    * Insert Tag
    */
    public function insertTag(Tag\Tag $tag)
    {   
        try{
            //Names
            $name = new Common\Name();

            //Insert into main table
            $sql = "INSERT INTO tag VALUES(null)";
            $statement = $this-&gt;connection-&gt;prepare($sql);
            $statement-&gt;execute();

            //Get main table id
            $tagId = $this-&gt;connection-&gt;lastInsertId();

            //Insert into audit tag table
            $sql = "INSERT INTO tag_audit(behaviour,action_peformed,state,tag_id) VALUES(:behaviour,'insert','P',:tag_id)";
            $statement = $this-&gt;connection-&gt;prepare($sql);
            $statement-&gt;bindValue(':behaviour',$tag-&gt;getBehaviour(),PDO::PARAM_STR);
            $statement-&gt;bindValue(':tag_id',$tagId,PDO::PARAM_INT);
            $statement-&gt;execute();

            //Insert into names
            $name-&gt;setNames($tag-&gt;getNames()[0]);
            $values = $name-&gt;getBulkValue(
                $name-&gt;getValue(),
                ["'insert'","'P'",$tagId]
            ); 

            $sql = "INSERT INTO tag_name_audit(name,language,action_peformed,state,tag) VALUES".$values;
            $statement = $this-&gt;connection-&gt;prepare($sql);
            $statement-&gt;execute();

            $response = ['status'=&gt;200,'Message'=&gt;'Successfully inserted.'];

        }catch(\Exception $e){

             $sql = "DELETE FROM tag WHERE id_tag = :id_tag";
             $statement = $this-&gt;connection-&gt;prepare($sql);
             $statement-&gt;bindValue(":id_tag",$tagId,PDO::PARAM_INT);
             $statement-&gt;execute();


             $sql = "DELETE FROM tag_audit WHERE tag_id = :id_tag";
             $statement = $this-&gt;connection-&gt;prepare($sql);
             $statement-&gt;bindValue(":id_tag",$tagId,PDO::PARAM_INT);
             $statement-&gt;execute();

             $response = ['status'=&gt;409,'Message'=&gt;'Error '.$e-&gt;getCode()];
        }
        $tag-&gt;setResponse($response);
    }


I don't like how much code there is and i tought of having some mapper helper classes which will handle at least a portion of it.But still i don't think that it's the right solution.

Also since i parse the bulk value over an array i know that it's not secure ....how could i solve that part?

So the question is how could i improve it?
Use a transaction, so you won't need all the manual delete queries
Use shorter PDO syntax.
Log errors
Use prepared statements


In the end it should be like

public function insertTag(Tag\Tag $tag)
{   
    try{
        //Names
        $name = new Common\Name();

        $pdo-&gt;beginTransaction();

        //Insert into main table
        $this-&gt;connection-&gt;query("INSERT INTO tag VALUES(null)");

        //Get main table id
        $tagId = $this-&gt;connection-&gt;lastInsertId();

        //Insert into audit tag table
        $sql = "INSERT INTO tag_audit(behaviour,action_peformed,state,tag_id) VALUES(?,'insert','P',?)";
        $this-&gt;connection-&gt;prepare($sql)-&gt;execute([$tag-&gt;getBehaviour(), $tagId]);

        //Insert into names
        // you should be using prepared statements here
        $sql = "INSERT INTO tag_name_audit(name,language,action_peformed,state,tag) VALUES (?,?,?,?,?)";
        $statement = $this-&gt;connection-&gt;prepare($sql);
        foreach (whatever function supplies the name as $name) {
            $statement-&gt;execute([$name]);
        }
        $response = ['status'=&gt;200,'Message'=&gt;'Successfully inserted.'];

        $pdo-&gt;commit();
    }catch(\Exception $e){

         $pdo-&gt;rollback();
         error_log($e);
         $response = ['status'=&gt;409,'Message'=&gt;'Error '.$e-&gt;getCode()];
    }
    $tag-&gt;setResponse($response);
}