> For the complete documentation index, see [llms.txt](https://polisek-scripts.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polisek-scripts.gitbook.io/untitled/code-examples/job-system-create-job-in-db.md).

# Job system - Create job in DB

Here is an example of code that can be used to create pls\_jobsystem ([link](https://github.com/polisek/pls_jobsystem/)) directly in the job database.

**Valid for ESX only**

1\) Go to server.lua - Find event pls\_jobsystem:server:saveNewJob

Insert the code registerJobInDatabase(jobData) under the SaveJobs() function

```lua
       if not IsJobExist(jobData.job) then
                    table.insert(Jobs, jobData)
                    lib.notify(src, {
                        title="Hell yeah!",
                        description="A new job has been created!",
                        type="success"
                    })
                    SaveJobs()

                registerJobInDatabase(jobData)
            else
```

Then, at the end of server.lua, insert the new function we will create.

```lua
function registerJobInDatabase(data)

end
```

What does the data value contain?&#x20;

```lua
data.label = Job label - STRING
data.coords = Player coords - VECTOR3
data.job = job name - STRING
data.area = Area ( size) - INT(NUMBER)
```

Function registerJobInDatabase(data)<br>

```lua
function registerJobInDatabase(data)
    MySQL.query('SELECT * FROM jobs WHERE name = ?', { data.job }, function(result)
            if #result == 0 then
                -- If job not exist
                MySQL.insert('INSERT INTO jobs (name, label, whitelisted) VALUES (?, ?, ?)', 
                { data.job, data.label, 0 }, function(insertId)
                    if insertId then
                        print("Job successfully inserted with ID: " .. insertId)
    
                        -- Define grades
                        local grades = {
                            {job_name = data.job, grade = 0, name = 'newbie', label = 'Newbie', salary = 200, skin_male = '{}', skin_female = '{}'},
                            {job_name = data.job, grade = 1, name = 'employe', label = 'Employe', salary = 400, skin_male = '{}', skin_female = '{}'},
                            {job_name = data.job, grade = 2, name = 'manage', label = 'Manage', salary = 500, skin_male = '{}', skin_female = '{}'},
                            {job_name = data.job, grade = 3, name = 'boss', label = 'Boss', salary = 1000, skin_male = '{}', skin_female = '{}'}
                        }
    
                        -- Insert grades
                        for _, grade in ipairs(grades) do
                            MySQL.insert('INSERT INTO job_grades (job_name, grade, name, label, salary, skin_male, skin_female) VALUES (?, ?, ?, ?, ?, ?, ?)', 
                            { grade.job_name, grade.grade, grade.name, grade.label, grade.salary, grade.skin_male, grade.skin_female }, function(gradeInsertId)
                                if gradeInsertId then
                                    print("Grade successfully inserted with ID: " .. gradeInsertId)
                                else
                                    print("Failed to insert grade: " .. grade.name)
                                end
                            end)
                        end
                    else
                        print("Failed to insert job")
                    end
                end)
            else
                print("Job already exists")
            end
        end)
end
```

Don't forget to add to **fxmanifest.lua**

```
server_scripts {
 "@oxmysql/lib/MySQL.lua", -- This line on start 
}
```

#### IMPORTANT&#x20;

**This code assumes that you are using oxmysql <https://github.com/overextended/oxmysql>**
