• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Dodawanie wyniku funkcji do bazy danych

Aruba Cloud - Virtual Private Server VPS
0 głosów
282 wizyt
pytanie zadane 30 stycznia 2022 w PHP przez trixter310 Obywatel (1,440 p.)

Witam napisałem stronę z systemem logowania i rejestracji, na której po zalogowaniu wchodzi się na swój profil. Na profilu dodałem podstronę o nazwie kalkulator, która oblicza dzienne zapotrzebowanie kaloryczne, które trzeba spożyć w zależności od oczekiwanego efektu. Teraz moje pytanie: Jak spowodować, żeby dane które wyświetlają się po wyliczeniach trafiły do bazy danych, żeby można było później skorzystać z nich w projekcie?

<?php
  
    session_start();
      
    if (!isset($_SESSION['zalogowany']))
    {
        header('Location: index.php');
        exit();
    }
    require_once "connection.php";
 
    $polaczenie = new mysqli($host, $db_user, $db_password, $db_name);
    if ($polaczenie->connect_errno)
    {
       throw new Exception(mysqli_connect_errno());
    }
    $sql = "SELECT gender FROM uzytkownicy WHERE id='".$_SESSION['zalogowany_id']."'";
    if ($rezultat = $polaczenie->query($sql))
    {
       $plec = $_SESSION['gender'];
       var_dump($plec);
    } 
 
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<?php include('templates/head.php'); ?> 
 
<script>
    function calc() {
    let A = document.getElementById("age").value;
    let W = document.getElementById("weight").value;
    let H = document.getElementById("height").value;
  
    let S, R, activity;
    let P = "<?php echo $plec; ?>";
  
  
   
    if (document.getElementById("age").value.length == 0) {
  
        document.getElementById("ATT").style = "color: red;"
        document.getElementById("WTT").style = "display: none;"
        document.getElementById("HTT").style = "display: none;"
  
        return
  
    }
  
    if (document.getElementById("weight").value.length == 0) {
  
        document.getElementById("ATT").style = "display: none;"
        document.getElementById("HTT").style = "display: none;"
        document.getElementById("WTT").style = "color: red;"
          
        return
  
    }
  
    if (document.getElementById("height").value.length == 0) {
  
        document.getElementById("ATT").style = "display: none;"
        document.getElementById("WTT").style = "display: none;"
        document.getElementById("HTT").style = "color: red;"
          
        return
  
    }
  
    if (P == "male")
    S =  Math.round((9.99*W)+(6.25*H)-(4.92*A)+5);
    else if (P == "female")
    S =  Math.round((9.99*W)+(6.25*H)-(4.92*A)-161);

    
    if (document.getElementById("0").selected) {
        R = S
    }
  
    
    if (document.getElementById("1").selected) {
        R = S*1.2
        activity = "1. Niewielka/Brak treningów"
  
    }
  
   
    if (document.getElementById("2").selected) {
        R = S*1.35
        activity = "2. Niska aktywność (treningi 1-3 razy/tydzień)"
    }
  
     
    if (document.getElementById("3").selected) {
        R = S*1.55
        activity = "3. Średnia aktywność (codzienna aktywność lub intensywne treningi 3-4 razy/tydzień)"
  
    }
      
    
    if (document.getElementById("4").selected) {
        R = S*1.75
        activity = "4. Wysoka aktywność (intensywne treningi 6-7 razy/tydzień)"
  
    }
  
  
    if (document.getElementById("5").selected) {
        R = S*1.95
        activity = "5. Bardzo wysoka aktywność (codzienne bardzo intensywne treningi lub praca fizyczna)"
  
    }
  
    document.getElementById("ATT").style = "display: none;"
    document.getElementById("HTT").style = "display: none;"
    document.getElementById("WTT").style = "display: none;"
  
    document.getElementById("age").placeholder = " "
    document.getElementById("weight").placeholder = "Kg"
    document.getElementById("height").placeholder = "Cm"
  
    document.getElementById("a").innerHTML = Math.round(R)
    document.getElementById("b").innerHTML = Math.round(R*0.80)
    document.getElementById("c").innerHTML = Math.round(R*1.20)
  
  
    document.getElementById("ak").innerHTML = Math.round(R)
    document.getElementById("ak-2").innerHTML = Math.round(S)
    document.getElementById("a-1k").innerHTML = Math.round((R*0.80)*0.25)
    document.getElementById("a-1g").innerHTML = Math.round(((R*0.80)*0.25)/4)
    document.getElementById("a-2k").innerHTML = Math.round((R*0.80)*0.50)
    document.getElementById("a-2g").innerHTML = Math.round(((R*0.80)*0.50)/4)
    document.getElementById("a-3k").innerHTML = Math.round((R*0.80)*0.25)
    document.getElementById("a-3g").innerHTML = Math.round(((R*0.80)*0.25)/9)
  
  
  
  
  
    document.getElementById("bk").innerHTML = Math.round(R)
    document.getElementById("bk-2").innerHTML = Math.round(S)
    document.getElementById("b-1k").innerHTML = Math.round((R*0.80)*0.25)
    document.getElementById("b-1g").innerHTML = Math.round(((R*0.80)*0.25)/4)
    document.getElementById("b-2k").innerHTML = Math.round((R*0.80)*0.50)
    document.getElementById("b-2g").innerHTML = Math.round(((R*0.80)*0.50)/4)
    document.getElementById("b-3k").innerHTML = Math.round((R*0.80)*0.25)
    document.getElementById("b-3g").innerHTML = Math.round(((R*0.80)*0.25)/9)
    document.getElementById("bs").innerHTML = Math.round(R-(R*0.80))
  
  
    document.getElementById("ck").innerHTML = Math.round(R)
    document.getElementById("ck-2").innerHTML = Math.round(S)
    document.getElementById("c-1k").innerHTML = Math.round((R*1.20)*0.25)
    document.getElementById("c-1g").innerHTML = Math.round(((R*1.20)*0.25)/4)
    document.getElementById("c-2k").innerHTML = Math.round((R*1.20)*0.50)
    document.getElementById("c-2g").innerHTML = Math.round(((R*1.20)*0.50)/4)
    document.getElementById("c-3k").innerHTML = Math.round((R*1.20)*0.25)
    document.getElementById("c-3g").innerHTML = Math.round(((R*1.20)*0.25)/9)
    document.getElementById("cs").innerHTML = Math.round((R*1.20)-R)
  
    document.getElementById("Final").style = " margin: auto; margin-top: 1em; border-radius: 20px; margin-bottom: 30px"
  
  
    if (document.getElementById("more").selected) {
        document.getElementById("less-text-1").style = "display: none;"
        document.getElementById("less-text-2").style = "display: none;"
        document.getElementById("constant-text-1").style = "display: none;"
        document.getElementById("constant-text-2").style = "display: none;"
        document.getElementById("more-text-1").style = "display: block;"
        document.getElementById("more-text-2").style = "display: table-row;"
        document.getElementById("more-text-1").scrollIntoView();
        dataLayer.push({
            'event': 'calc',
            'cel': 'chce przytyć',
            'waga': W,
            'wiek': A,
            'wzrost' : H,
            'płeć' : P,
            'aktywność' : activity
        });
  
    }
    if (document.getElementById("less").selected) {
        document.getElementById("more-text-1").style = "display: none;"
        document.getElementById("more-text-2").style = "display: none;"
        document.getElementById("constant-text-1").style = "display: none;"
        document.getElementById("constant-text-2").style = "display: none;"
        document.getElementById("less-text-1").style = "display: block;"
        document.getElementById("less-text-2").style = "display: table-row;"
        document.getElementById("less-text-1").scrollIntoView();
        dataLayer.push({
            'event': 'calc',
            'cel': 'chce schudnąć',
            'waga': W,
            'wiek': A,
            'wzrost' : H,
            'płeć' : P,
            'aktywność' : activity
        });
  
    }
    if (document.getElementById("constant").selected) {
        document.getElementById("more-text-1").style = "display: none;"
        document.getElementById("more-text-2").style = "display: none;"
        document.getElementById("less-text-1").style = "display: none;"
        document.getElementById("less-text-2").style = "display: none;"
        document.getElementById("constant-text-1").style = "display: block;"
        document.getElementById("constant-text-2").style = "display: table-row;"
        document.getElementById("constant-text-1").scrollIntoView();
        dataLayer.push({
            'event': 'calc',
            'cel': 'utrzymanie wagi',
            'waga': W,
            'wiek': A,
            'wzrost' : H,
            'płeć' : P,
            'aktywność' : activity
        });
  
       
    }
} 
</script>
</head>

 

komentarz 30 stycznia 2022 przez VBService Ekspert (256,600 p.)

Jeżeli "ustawiasz" wartość dla zmiennej $plec, za pomocą $_SESSION

$plec = $_SESSION['gender'];

to Moim zdaniem nie potrzebujesz tu łączenia się z bazą danych.

 

<?php
   
    session_start();
       
    if (!isset($_SESSION['zalogowany']))
    {
        header('Location: index.php');
        exit();
    }
    
    $plec = $_SESSION['gender'];

?>
<!DOCTYPE html>

    ... reszta kodu

 

1 odpowiedź

0 głosów
odpowiedź 30 stycznia 2022 przez trixter310 Obywatel (1,440 p.)

Drugą część kodu wysyłam w odpowiedzi, bo za dużo znaków

<body>
    <section class="home">
    <div class="container2">
    <div class="title">Oblicz zapotrzebowanie kaloryczne</div>
    <div class="content">
    <div class="user-details">
        
  <div class="input-box">
<span class="details" id="AT"> Wiek</span>
    <input type="number" min="0" id="age" placeholder=""
    style="border-width: 2px; border-radius: 5px;">
    <div style="display: none;" id="ATT">Wpisz prawidłowy wiek</div>
    </div>
  
    <div class="input-box">
<span class="details">Waga</span>
    <input type="number" min="0" id="weight"
    style="border-width: 2px; border-radius: 5px;" placeholder="Kg">
    <div style="display: none;" id="WTT">Wpisz prawidłową wagę</div>
    </div>
  
    <div class="input-box">
<span class="details">Wzrost</span>
    <input type="number" min="0" id="height"
    style="border-width: 2px; border-radius: 5px;" placeholder="Cm">
    <div style="display: none;" id="HTT">Wpisz prawidłowy wzrost</div>
    </div>
  
  
  
  
    <div class="input-box">
<span class="details">Poziom aktywności</span>
<select class="I2">
  <option value="1" id="1">1. Niewielka/Brak treningów</option>
  <option value="2" id="2">2. Niska aktywność (treningi 1-3 razy/tydzień)</option>
  <option value="3" id="3">3. Średnia aktywność (codzienna aktywność lub intensywne treningi 3-4 razy/tydzień)</option>
  <option value="4" id="4">4. Wysoka aktywność (intensywne treningi 6-7 razy/tydzień)</option>
  <option value="5" id="5">5. Bardzo wysoka aktywność (codzienne bardzo intensywne treningi lub praca fizyczna)</option>
  <option value="0" id="0" style="display:none;">1. Bazowe zapotrzebowanie kaloryczne (BMR)</option>
</select>
</div>
  
<div class="input-box">
<span class="details">Jaki jest cel Twojej diety?</span>
<select class="I2"> 
  <option value="less" id="less">1. Chce schudnąć</option>
  <option value="constant" id="constant">2. Chcę utrzymać wagę</option>
  <option value="more" id="more">3. Chcę przytyć</option>
</select>
</div>
  
</div>
<div class="button">
<button id="button-calc" type="button" onclick="calc()">Oblicz</button>
</div>
</div>
</div>






    <div id="Final" class="container2" style="display: none;">

        <div id="less-text-1">
            <h5 style="padding: 1em 0; font-size: 2em;">Utrata wagi</h5>
            <p>Twój wskaźnik BMR to:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="bk-2">&nbsp</span><span> kalorii dziennie</span></p>

            <p style="padding-top: 1em;">Twoje zapotrzebowanie kaloryczne po uwzględnieniu aktywności to:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="bk">&nbsp</span><span> kalorii dziennie</span></p>
          

            <p style="padding-top: 2em;">Jeśli chcesz zredukować wagę powinieneś spożywać ok.:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="b">&nbsp</span><span> kalorii dziennie</span></p>
            <p style="font-weight: 600; font-size: x-large; color: red;">-<span id="bs">&nbsp</span><span> kalorii dziennie</span></p>

   

            <h5 style="padding: 3em 0 1em 0; font-size: 1.2em;">Rozkład makroelementów dla diety na redukcje</h5>

            <div>
                <ul class="macro-list">
                    <li>
                    <p>
                        <strong>50% węglowodanów: </strong>
                        <span id="b-2k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="b-2g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>

                    <li>                
                    <p>
                        <strong>25% białka: </strong> 
                        <span id="b-1k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="b-1g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>

                    <li>
                    <p>
                        <strong>25% tłuszczy: </strong>
                        <span id="b-3k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="b-3g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>
                </ul>
            </div>
            <div class="button">
            <button>Zapisz zapotrzebowanie</button>
            </div>
        </div>
        
        <table id="less-text-2" style="margin-top: 15px;">
     
        </table>

  

         <div id="more-text-1">
            <h5 style="padding: 1em 0; font-size: 2em;">Zwiększenie wagi</h5>
            <p>Twój wskaźnik BMR to:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="ck-2">&nbsp</span><span> kalorii dziennie</span></p>

            <p style="padding-top: 1em;">Twoje zapotrzebowanie kaloryczne po uwzględnieniu aktywności to:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="ck">&nbsp</span><span> kalorii dziennie</span></p>
          

            <p style="padding-top: 2em;">Jeśli chcesz zwiększyć wagę powinieneś spożywać ok.:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="c">&nbsp</span><span> kalorii dziennie</span></p>
            <p style="font-weight: 600; font-size: x-large; color: red;">+<span id="cs">&nbsp</span><span> kalorii dziennie</span></p>

    


            <h5 style="padding: 3em 0 1em 0; font-size: 1.2em;">Rozkład makroelementów dla diety na masę</h5>

            <div>
                <ul class="macro-list">
                    <li>
                    <p>
                        <strong>50% węglowodanów: </strong>
                        <span id="c-2k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="c-2g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>

                    <li>                
                    <p>
                        <strong>25% białka: </strong> 
                        <span id="c-1k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="c-1g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>

                    <li>
                    <p>
                        <strong>25% tłuszczy: </strong>
                        <span id="c-3k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="c-3g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>
                </ul>
            </div>

            <div class="button">
            <button>Zapisz zapotrzebowanie</button>
            </div>

        </div>
     

        <table id="more-text-2">
      
        </table>

        <div id="constant-text-1">
            <h5 style="padding: 1em 0; font-size: 2em;">Utrzymanie wagi</h5>
            <p>Twój wskaźnik BMR to:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="ak-2">&nbsp</span><span> kalorii dziennie</span></p>

            <p style="padding-top: 1em;">Twoje zapotrzebowanie kaloryczne po uwzględnieniu aktywności to:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="ak">&nbsp</span><span> kalorii dziennie</span></p>
          

            <p style="padding-top: 2em;">Jeśli chcesz utrzymać wagę powinieneś spożywać ok.:</p>
            <p style="font-weight: 600; font-size: x-large;"><span id="a">&nbsp</span><span> kalorii dziennie</span></p>


            <h5 style="padding: 3em 0 1em 0; font-size: 1.2em;">Rozkład makroelementów dla diety na utrzymanie wagi</h5>

            <div>
                <ul class="macro-list">
                    <li>
                    <p>
                        <strong>50% węglowodanów: </strong>
                        <span id="a-2k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="a-2g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>

                    <li>                
                    <p>
                        <strong>25% białka: </strong> 
                        <span id="a-1k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="a-1g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>

                    <li>
                    <p>
                        <strong>25% tłuszczy: </strong>
                        <span id="a-3k" class="SS">&nbsp</span><span> kcal = </span>
                        <span id="a-3g" class="SS">&nbsp</span><span>g</span>
                    </p>
                    </li>
                </ul>
            </div>

            
            <div class="button">
            <button>Zapisz zapotrzebowanie</button>
            </div>

        </div>
   
<table id="constant-text-2">

            
      </table>
  
    </div>
    </section>

    </body>
</html>

 

Podobne pytania

+1 głos
1 odpowiedź 321 wizyt
pytanie zadane 31 stycznia 2022 w PHP przez trixter310 Obywatel (1,440 p.)
+1 głos
3 odpowiedzi 3,759 wizyt
pytanie zadane 2 kwietnia 2021 w PHP przez maciek3621 Nowicjusz (180 p.)
0 głosów
2 odpowiedzi 678 wizyt
pytanie zadane 21 marca 2022 w PHP przez pazo1313 Nowicjusz (140 p.)

93,329 zapytań

142,323 odpowiedzi

322,400 komentarzy

62,662 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Wprowadzenie do ITsec, tom 1 Wprowadzenie do ITsec, tom 2

Można już zamawiać dwa tomy książek o ITsec pt. "Wprowadzenie do bezpieczeństwa IT" - mamy dla Was kod: pasja (użyjcie go w koszyku), dzięki któremu uzyskamy aż 15% zniżki! Dziękujemy ekipie Sekuraka za fajny rabat dla naszej Społeczności!

...