0

I'm just starting to learn PHP and my academic teacher gave my group a task to create a fully functional site that stores data about some fictitious scientific publications. My task from now is to implement functionality about adding a new publication to a database. Here lies my problem. Another person in my group created a front end part of a site in jquery that creates rows to input data about an author of an article. We are adding his name,surname,affiliation and his share(%) in creating an article. It looks like this:

enter image description here

Database:

enter image description here

Code for function that creates new rows :

$(document).ready(function () {
    $(".add-row").click(function () {
        var authorFirstname = $("#authorFirstname").val();
        var authorLastname = $("#authorLastname").val();
        var authorAffiliation = $("#authorAffiliation").val();
        var authorPercentage = $("#authorPercentage").val();
        var markupSearch = "<tr><td><input type='checkbox' name='record'></td><td>" + authorFirstname + "</td><td>" + authorLastname + "</td><td>" + authorAffiliation + "</td></tr>";
        var markupAdd = "<tr><td><input type='checkbox' name='record'></td><td>" + authorFirstname + "</td><td>" + authorLastname + "</td><td>" + authorAffiliation + "</td><td>" + authorPercentage + "%</td></tr>";
        $("table.markupSearch tbody").append(markupSearch);
        $("table.markupAdd tbody").append(markupAdd);
    });

    $(".delete-row").click(function () {
        $("table tbody").find('input[name="record"]').each(function () {
            if ($(this).is(":checked")) {
                $(this).parents("tr").remove();
            }
        });
    });
});

Then we have a form:

<div class="container page-content search">

    <div class="row">
        <div class="col-md-12">
            <h1 class="header">Dodaj publikację</h1>
        </div>
    </div>

    <div class="row">

        <div class="col-md-10 mx-auto">
           
            <form method="post">

                <div class="form-row">
                    <div class="col">
                        <div class="md-form">
                            <input type="text" id="addTitlePublication" name="title" class="form-control">
                            <label for="addTitlePublication">Tytuł publikacji</label>
                        </div>
                    </div>
                </div>

                <div class="form-row">
                    <div class="col">
                        <div class="md-form">
                            <input type="date" id="addDate" name="date" class="form-control">
                            <label for="addDate">Data publikacji</label>
                        </div>
                    </div>
                    <div class="col">
                        <div class="md-form">
                            <input type="number" id="addDOI" name="doi" class="form-control">
                            <label for="addDOI">DOI</label>
                        </div>
                    </div>
                    <div class="col">
                        <div class="md-form">
                            <input type="number" min="1" max="1000" id="addMinisterialPoints" name="points" class="form-control">
                            <label for="addMinisterialPoints">Punkty ministerialne</label>
                        </div>
                    </div>
                </div>

                <div class="form-row">
                    <h5 class="mt-3">Miejsce publikacji:</h5>
                </div>
                
                <div class="form-row">
                    <div class="col">
                        <div class="md-form">
                            <input type="text" id="publicationMagazine" name="magazine" class="form-control"  required>
                            <label for="publicationMagazine">Nazwa czasopisma</label>
                        </div>
                    </div>
                    <div class="col">
                        <div class="md-form">
                            <input type="text" id="publicationConference" name="conference" class="form-control" required>
                            <label for="publicationConference">Nazwa konferencji</label>
                        </div>
                    </div>
                </div>

                <h4 class="mt-5">Autorzy publikacji</h4>

                <table class="authors markupAdd">
                    <thead>
                        <tr>
                            <th><button type="button" class="delete-row" title="Usuń wybranych autorów"><i class="fas fa-trash-alt"></i></button></th>
                            <th>Imię</th>
                            <th>Nazwisko</th>
                            <th>Afiliacja</th>
                            <th>Udział procentowy</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>

                        </tr>
                    </tbody>
                </table>

                <h5 class="mt-3">Dodawanie nowego autora:</h5>
                <div class="form-row">
                    <div class="col-md-3">
                        <div class="md-form">
                            <input type="text" id="authorFirstname" name="name" placeholder="Imię" class="form-control">
                            <label for="authorFirstname"></label>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="md-form">
                            <input type="text" id="authorLastname" name="surname" placeholder="Nazwisko" class="form-control">
                            <label for="authorLastname"></label>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="md-form">
                            <input type="text" id="authorAffiliation" name="afiliation" placeholder="Afiliacja" class="form-control">
                            <label for="authorAffiliation"></label>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="md-form">
                            <input type="number" min="1" max="100" id="authorPercentage" name="share" placeholder="Udział procentowy" class="form-control">
                            <label for="authorPercentage"></label>
                        </div>
                    </div>
                    <div class="col-md-1">
                        <div class="md-form">
                            <button type="button" class="btn btn-add add-row mb-4"><i class="fas fa-plus"></i></button>
                        </div>
                    </div>
                </div>


                <div class="text-center text-md-left mt-3">
                    <button class="btn btn-dark">Dodaj publikację</button>
                </div>
            </form>
            
        </div>

    </div>

</div>

And my code for adding basic data:

<php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $conn = getDB();
    
    $sql = "INSERT INTO records (title, date, conference, doi, points)
            VALUES ('" . $_POST['title'] . "','" . $_POST['date'] . "','" . $_POST['conference'] . "','" . $_POST['doi'] . "','" . $_POST['points'] . "')";
    $sqla = "INSERT INTO authors (name, surname, afiliation, share) VALUES ('" . $_POST['name'] . "','" . $_POST['surname'] . "','" . $_POST['afiliation'] . "','" . $_POST['share'] . "')";


    $results = mysqli_query($conn, $sql);
    $results = mysqli_query($conn, $sqla);

    if ($results === false) {

        echo mysqli_error($conn);

    } else {

        $id = mysqli_insert_id($conn);
        echo "added record id: $id";

    }

}
?>

Could someone please help me understand how to insert that data from these rows to a database? And how to link authors of an article with an article itself. Another thing, maybe I structured my database wrong and I need some changes there?

All in all thank you for your answers in advance. Please forgive me my English if I made any mistakes. I tried to decribe my problem as clearly as I could.

My question is different to This answer. I know basics for insert statement. I don't know how to insert data from dynamically generated table rows, which are made the way I have shown. If I try to make a basic insert statement, it only adds first row of a Author's(Autorzy Publikacji) table.

Sangreal
  • 11
  • 2
  • Does this answer your question? [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) – Dharman Nov 24 '19 at 21:49
  • I'm sorry but not really. I learned there that I should use prepared statements, but I know basics of an insert statement. But when I try to execute such statement, and I have multiple rows created and filled with multiple authors, I only post the first one, and I want to insert all authors under same record_id. – Sangreal Nov 24 '19 at 22:01
  • Welcome to Stack Overflow. None of your code or HTML is configured to POST data to your PHP. It's not clear what you want to happen when. Please clarify your question. – Twisty Nov 25 '19 at 01:05

0 Answers0