Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Formularz mysql
UszaTek
post 12.12.2021, 22:02:40
Post #1





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 15.07.2019

Ostrzeżenie: (0%)
-----


Witam.
Mam kilka problemów z formularzem.
* Odświeżanie strony powoduje dodanie wpisu do bazy.
* Dodawanie wartości do bazy działa tylko dla opcji " temperatura"

* Czy jest możliwość zrobienia aby ' input typ number' miał widoczne strzalki góra/dół caly czas a nie tylko jak najedziemy kursorem?

Ss'y



Kod
   <?php
  
        $conn = mysqli_connect("xxx", "xxx", "xxx", "xxx");
          
        if($conn === false)
        {
            die("ERROR: Could not connect. "
                . mysqli_connect_error());
        }
        
        $temperature =  $_REQUEST['temperature'];
            $sql = "INSERT INTO stat (temperature) VALUES ($temperature)";
        
        $humidity =  $_REQUEST['humidity'];
            $sql = "INSERT INTO stat (humidity) VALUES ($humidity)";
        
        $temperature_water =  $_REQUEST['temperature_water'];
            $sql = "INSERT INTO stat (humidity) VALUES ($temperature_water)";
        
        $ph =  $_REQUEST['ph'];
            $sql = "INSERT INTO stat (humidity) VALUES ($ph)";

        if(mysqli_query($conn, $sql))
        {
            echo "<h3>data stored in a database successfully. "
                . " Please browse your localhost php my admin"
                . " to view the updated data</h3>";
  
            echo nl2br("\n$temperature");
        }
        else
        {
            echo "ERROR: Hush! Sorry $sql. "
                . mysqli_error($conn);
        }
          
        mysqli_close($conn);
?>


Kod
<form action="settings.php" method="post">
    <div class="tekst">Temperature</div>
    <input type="number" name="temperature" class="rectangle-settings" value=<? echo $temper;?>>
</form>


Ten post edytował UszaTek 12.12.2021, 22:07:04
Go to the top of the page
+Quote Post
aras785
post 13.12.2021, 01:45:05
Post #2





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


Pokaż więcej tego html, a bo w formularzu nie widzę więc pól smile.gif
Go to the top of the page
+Quote Post
UszaTek
post 13.12.2021, 05:30:11
Post #3





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 15.07.2019

Ostrzeżenie: (0%)
-----


Cytat(aras785 @ 13.12.2021, 01:45:05 ) *
Pokaż więcej tego html, a bo w formularzu nie widzę więc pól smile.gif

Cały formularz:
Kod

<form action="settings.php" method="post">
    <div class="tekst">Temperature</div>
    <input type="number" name="temperature" class="rectangle-settings" value=<? echo $temperature;?>>
</form>

<form action="settings.php" method="post">
    <div class="tekst">Humidity</div>
    <input type="number" name="humidity" class="rectangle-settings" value=<? echo $humidity;?>>

</form>
<form action="settings.php" method="post">
    <div class="tekst">pH</div>
    <input type="number" name="ph" class="rectangle-settings" value=<? echo $ph;?>>
</form>


Ten post edytował UszaTek 13.12.2021, 05:32:21
Go to the top of the page
+Quote Post
viking
post 13.12.2021, 06:46:04
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


A jak to ma działać? Ma zapisywać dowolną jedną z podanych wartości? To sprawdzaj dodatkowo czy jest większa od 0. Teraz to działa losowo.


--------------------
Go to the top of the page
+Quote Post
trueblue
post 13.12.2021, 08:00:45
Post #5





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


To są 3 formularze, a nie jeden.


--------------------
Go to the top of the page
+Quote Post
UszaTek
post 13.12.2021, 08:23:13
Post #6





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 15.07.2019

Ostrzeżenie: (0%)
-----


Cytat(viking @ 13.12.2021, 06:46:04 ) *
A jak to ma działać? Ma zapisywać dowolną jedną z podanych wartości? To sprawdzaj dodatkowo czy jest większa od 0. Teraz to działa losowo.


Próbuje osiągnąć, żeby można było dodać jeden rekord do każdej tabeli, a jak już jest w tabeli rekord to aby zmieniało jego wartość a nie dodawało kolejny.

Oraz jak chce dodać np 'ph' lub 'Temperature-water' to nic nie dodaje. Jedynie moge dodawać pierwsza opcje z formularza


Ten post edytował UszaTek 13.12.2021, 08:24:39
Go to the top of the page
+Quote Post
viking
post 13.12.2021, 09:06:11
Post #7





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Dodaj sobie jakiś typ, rodzaj który będzie unikalny. A potem https://dev.mysql.com/doc/refman/8.0/en/ins...-duplicate.html


--------------------
Go to the top of the page
+Quote Post
UszaTek
post 13.12.2021, 10:07:44
Post #8





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 15.07.2019

Ostrzeżenie: (0%)
-----


Cytat(viking @ 13.12.2021, 09:06:11 ) *
Dodaj sobie jakiś typ, rodzaj który będzie unikalny. A potem https://dev.mysql.com/doc/refman/8.0/en/ins...-duplicate.html


Możesz nakierowaćnie jak stworzyć unikalny tryb/rodzaj?
Go to the top of the page
+Quote Post
gino
post 13.12.2021, 10:24:10
Post #9





Grupa: Zarejestrowani
Postów: 319
Pomógł: 52
Dołączył: 18.02.2008

Ostrzeżenie: (0%)
-----


Zapytam za @trueblue po co 3 formularze?
Jeden formularz z 3 (4?) polami, jeden insert do bazy, dla każdej wartości osobne pole w tabeli.
Skąd pobierasz $temperature_water? Nie ma tego w formularzu. Czemu to nie jest w osobnym polu tabeli? Skąd będziesz wiedział co jest temperaturą a co temperaturą wody?

"Odświeżanie strony powoduje dodanie wpisu do bazy"

Wyrzuć wszystkie echa z pliku settings.php, usuń wszystkie białe znaki (spacje, \n, \r itd). Tam ma być tylko walidacja i sanityzacja danych, połaczenie z bazą, wpis do tabeli, rozłaczenie i na końcu w zależności od powodzenia lub nie, zrób przekierowanie typu 302 (Moved Temporarily):

Kod
header("Location:https://www.twoja_strona.pl/twoja_strona_z_formularzem.php?status=1"); //1-wszystko ok, 0-nie zrobił się insert
exit();


Obsłuż tą zmienną $_GET['status'] w pliku gdzie masz formularz np.:
Kod
if(isset($_GET['status']))
{
  $message = $_GET['status'] ? "Przesłane" : "Bład zapisu do bazy";
  echo $message;
}
Go to the top of the page
+Quote Post
viking
post 13.12.2021, 10:39:40
Post #10





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


https://dev.mysql.com/doc/refman/8.0/en/cre...te-index-unique


--------------------
Go to the top of the page
+Quote Post
UszaTek
post 13.12.2021, 19:21:45
Post #11





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 15.07.2019

Ostrzeżenie: (0%)
-----


Cytat(aras785 @ 13.12.2021, 01:45:05 ) *
Pokaż więcej tego html, a bo w formularzu nie widzę więc pól smile.gif


Tutaj cały plik: https://pastebin.com/UrfTVA7u


Nie mogłem edytować.
Cały czas walczę z tym kodem.

Kod
<?php
    $conn = mysqli_connect("mysql1.ugu.pl", "db699487", "Mefedron12$", "db699487");
          
    if($conn === false)
    {
        die("ERROR: Could not connect. " .mysqli_connect_error());
    }
        
    $temperature =  $_REQUEST['temperature'];
        $sql = "INSERT INTO stat (temperature) VALUES ($temperature)";
    
    $humidity =  $_REQUEST['humidity'];
        $sql = "INSERT INTO stat (humidity) VALUES ($humidity)";
        
    $temperature-water-pot-1 =  $_REQUEST['temperature-water-pot-1'];
        $sql = "INSERT INTO stat (temperature-water-pot-3) VALUES ($temperature-water-pot-1)";
    $temperature-water-pot-2 =  $_REQUEST['temperature-water-pot-2'];
        $sql = "INSERT INTO stat (temperature-water-pot-3) VALUES ($temperature-water-pot-2)";
    $temperature-water-pot-3 =  $_REQUEST['temperature-water-pot-3'];
        $sql = "INSERT INTO stat (temperature-water-pot-3) VALUES ($temperature-water-pot-3)";
    $temperature-water-pot-4 =  $_REQUEST['temperature-water-pot-4'];
        $sql = "INSERT INTO stat (temperature-water-pot-4) VALUES ($temperature-water-pot-4)";
        
    $ph-pot-1 =  $_REQUEST['ph-pot-1'];
        $sql = "INSERT INTO stat (ph-pot-1) VALUES ($ph-pot-1)";
    $ph-pot-2 =  $_REQUEST['ph-pot-2'];
        $sql = "INSERT INTO stat (ph-pot-2) VALUES ($ph-pot-2)";
    $ph-pot-3 =  $_REQUEST['ph-pot-3'];
        $sql = "INSERT INTO stat (ph-pot-3) VALUES ($ph-pot-3)";
    $ph-pot-4 =  $_REQUEST['ph-pot-4'];
        $sql = "INSERT INTO stat (ph-pot-4) VALUES ($ph-pot-4)";
        
    if(mysqli_query($conn, $sql))
    {
        echo "<h3>data stored in a database successfully. "
        . " Please browse your localhost php my admin"
        . " to view the updated data</h3>";
  
        echo nl2br("$temperature\n $humidity");
    }
    else
    {
        echo "ERROR: Hush! Sorry $sql. "
            . mysqli_error($conn);
    }
    
    mysqli_close($conn);
?>


Zapytanie do bazy jakie wykonałem:

Kod
CREATE TABLE IF NOT EXISTS `stat`
(
  `temperature` int(11) NOT NULL,
  `humidity` int(11) NOT NULL,
  `temperature-water-pot-1` int(11) NOT NULL,
  `temperatuxre-water-pot-2` int(11) NOT NULL,
  `temperature-water-pot-2` int(11) NOT NULL,
  `temperature-water-pot-3` int(11) NOT NULL,
  `ph-pot-1` int(11) NOT NULL,
  `ph-pot-2` int(11) NOT NULL,
  `ph-pot-3` int(11) NOT NULL,
  `ph-pot-4` int(11) NOT NULL
)


W formularzu działa, że tylko temperature wysyła do bazy a temarature wody,ph,wilgotnośc juz nie.
Go to the top of the page
+Quote Post
viking
post 13.12.2021, 19:41:10
Post #12





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Jak Ci to ma działać skoro zdefiniowałeś kolumny not null a wysyłasz tylko 1? Już Ci mówiłem że masz stworzyć kolumnę np typ - najlepiej numeryczną - i masz tam wkładać wartości dla określonego typu.


--------------------
Go to the top of the page
+Quote Post
trueblue
post 13.12.2021, 19:46:21
Post #13





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


A może Autor odpowie na pytanie czy chce zapisywać jedną wartość, aktualnie edytowaną, czy wszystkie naraz?


--------------------
Go to the top of the page
+Quote Post
UszaTek
post 13.12.2021, 20:24:04
Post #14





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 15.07.2019

Ostrzeżenie: (0%)
-----


Cytat(trueblue @ 13.12.2021, 19:46:21 ) *
A może Autor odpowie na pytanie czy chce zapisywać jedną wartość, aktualnie edytowaną, czy wszystkie naraz?


Chcę jedną wartość.

O dziwo teraz pokazuje ładnie dane.

Kod


<?php
    $conn = mysqli_connect("xxu.pl", "db69948xxx7", "xxx$", "dx487"x);
          
    if($conn === false)
    {
        die("ERROR: Could not connect. " .mysqli_connect_error());
    }
        
    $temperature =  $_REQUEST['temperature'];
        $sql = "INSERT INTO stat (temperature) VALUES ($temperature)";
    
    $humidity =  $_REQUEST['humidity'];
        $sql = "INSERT INTO stat (humidity) VALUES ($humidity)";
        
    $TemperatureWaterPot1 =  $_REQUEST['TemperatureWaterPot1'];
        $sql = "INSERT INTO stat (TemperatureWaterPot1) VALUES ($TemperatureWaterPot1)";
        
    $TemperatureWaterPot2 =  $_REQUEST['TemperatureWaterPot2'];
        $sql = "INSERT INTO stat (TemperatureWaterPot2) VALUES ($TemperatureWaterPot2)";
        
    $TemperatureWaterPot3 =  $_REQUEST['TemperatureWaterPot3'];
        $sql = "INSERT INTO stat (TemperatureWaterPot3) VALUES ($TemperatureWaterPot3)";
        
    $TemperatureWaterPot4 =  $_REQUEST['TemperatureWaterPot4'];
        $sql = "INSERT INTO stat (TemperatureWaterPot4) VALUES ($TemperatureWaterPot4)";
        
    $pHPot1 =  $_REQUEST['pHPot1'];
        $sql = "INSERT INTO stat (pHPot1) VALUES ($pHPot1)";
        
    $pHPot2 =  $_REQUEST['pHPot2'];
        $sql = "INSERT INTO stat (pHPot2) VALUES ($pHPot2)";
        
    $pHPot3 =  $_REQUEST['pHPot3'];
        $sql = "INSERT INTO stat (pHPot3) VALUES ($pHPot3)";
        
    $pHPot4 =  $_REQUEST['pHPot4'];
        $sql = "INSERT INTO stat (pHPot4) VALUES ($pHPot4)";
        
    if(mysqli_query($conn, $sql))
    {
        echo "<h3>data stored in a database successfully. "
        . " Please browse your localhost php my admin"
        . " to view the updated data</h3>";
  
        echo nl2br("$temperature\n $humidity");
    }
    else
    {
        echo "ERROR: Hush! Sorry $sql. "
            . mysqli_error($conn);
    }
    
    mysqli_close($conn);
?>


Kod
<?
    $username="dxxx9487";
    $password="Mxxxxx";
    $database="db6xxxx487";

    mysql_connect('myxxxgu.pl',$username,$password);
    @mysql_select_db($database) or die("Nie odnaleziono bazy danych");
    $query="SELECT * FROM stat";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    echo "";

    $i=0;
    while ($i < $num)
    {
        $temperature=mysql_result($result,$i,"temperature");
        $humidity=mysql_result($result,$i,"humidity");
        
        $TemperatureWaterPot1=mysql_result($result,$i,"TemperatureWaterPot1");
        $TemperatureWaterPot2=mysql_result($result,$i,"TemperatureWaterPot2");
        $TemperatureWaterPot3=mysql_result($result,$i,"TemperatureWaterPot3");
        $TemperatureWaterPot4=mysql_result($result,$i,"TemperatureWaterPot4");
        
        $pHPot1=mysql_result($result,$i,"pHPot1");
        $pHPot2=mysql_result($result,$i,"pHPot2");
        $pHPot3=mysql_result($result,$i,"pHPot3");
        $pHPot4=mysql_result($result,$i,"pHPot4");

        echo "";
        $i++;
    }
?>


Kod
<table border="1" bordercolor="white" cellspacing="10" align="right" class="table">
        <tr>
            <td>
                <form action="settings.php" method="post">
                    <div class="temperature">Temperature</div> <input type="number" name="temperature" class="rectangle-settings" value=<? echo $temperature;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="humidity">Humidity</div> <input type="number" name="humidity" class="rectangle-settings" value=<? echo $humidity;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="temperature-water-pot-1">Temperature water</div> <input type="number" name="TemperatureWaterPot1" class="rectangle-settings" value=<? echo $TemperatureWaterPot1;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="pHPot1">pH</div> <input type="number" name="pHPot1" class="rectangle-settings" value=<? echo $pHPot1;?>>
                </form>
            </td>
            
            
            <td>
                <form action="settings.php" method="post">
                    <div class="temperature">Temperature</div> <input type="number" name="temperature" class="rectangle-settings" value=<? echo $temperature;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="humidity">Humidity</div> <input type="number" name="humidity" class="rectangle-settings" value=<? echo $humidity;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="temperature-water-pot-2">Temperature water</div> <input type="number" name="TemperatureWaterPot2" class="rectangle-settings" value=<? echo $TemperatureWaterPot2;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="pHPot2">pH</div> <input type="number" name="pHPot2" class="rectangle-settings" value=<? echo $pHPot2;?>>
                </form>
            </td>
        </tr>
        
        <tr>
            <td>
                <form action="settings.php" method="post">
                    <div class="temperature">Temperature</div> <input type="number" name="temperature" class="rectangle-settings" value=<? echo $temperature;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="humidity">Humidity</div> <input type="number" name="humidity" class="rectangle-settings" value=<? echo $humidity;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="temperature-water-pot-3">Temperature water</div> <input type="number" name="TemperatureWaterPot3" class="rectangle-settings" value=<? echo $TemperatureWaterPot3;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="pHPot3">pH</div> <input type="number" name="pHPot3" class="rectangle-settings" value=<? echo $pHPot3;?>>
                </form>
            </td>
            
        <td>
                <form action="settings.php" method="post">
                    <div class="temperature">Temperature</div> <input type="number" name="temperature" class="rectangle-settings" value=<? echo $temperature;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="humidity">Humidity</div> <input type="number" name="humidity" class="rectangle-settings" value=<? echo $humidity;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="temperature-water-pot-4">Temperature water</div> <input type="number" name="TemperatureWaterPot4" class="rectangle-settings" value=<? echo $TemperatureWaterPot4;?>>
                </form>
                
                <form action="settings.php" method="post">
                    <div class="pHPot4">pH</div> <input type="number" name="pHPot4" class="rectangle-settings" value=<? echo $pHPot4;?>>
                </form>
            </td>
    </tr>
    </table>


Live demo:
Kod
www.programbox.ugu.pl/settings.php


Ten post edytował UszaTek 13.12.2021, 21:11:52
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 24.04.2024 - 03:10