Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][HTML] Pobieranie rekordu z bazy do formularza
Forum PHP.pl > Forum > PHP
UszaTek
Witam.
Dość długo nie mogę sobie poradzić z problem aby wyświetlało rekord w formularzu.
Kod:
Kod
<form action="settings.php" method="post">
                    <label for="s1">Temperature: </label>
                        <div class="ui-spinner">
                            <input type="text" id="s1" name="TemperaturePot1" class="ui-spinner-box" autocomplete="off" value=<? echo '<p>'.$rows[1].'</p>'; ?>>
                            <button class="ui-spinner-up" type="button">?</button>
                            <button class="ui-spinner-down" type="button">?</button>
                        </div>
                </form>


Zdjecie do wglądu:



Próbowałem jeszcze tak:

Kod
<form action="settings.php" method="post">
                    <label for="s1">Temperature: </label>
                        <div class="ui-spinner">
                            <input type="text" id="s1" name="TemperaturePot1" class="ui-spinner-box" autocomplete="off" value=<? echo  printf("%s", $rows[0]); ?>>
                            <button class="ui-spinner-up" type="button">?</button>
                            <button class="ui-spinner-down" type="button">?</button>
                        </div>
                </form>


Lecz w tedy tylko " 0 " jest wświetlane.
viking
Już miałeś mówione że w value nie ma prawa być znaczników html.
UszaTek
Cytat(viking @ 1.01.2022, 07:35:55 ) *
Już miałeś mówione że w value nie ma prawa być znaczników html.


Ten kod nie ma html i cały czas wyświetla " 0 "

Kod
value=<? echo  printf("%s", $rows[0]); ?>
viking
Pewnie dlatego że cały czas losowo strzelasz funkcjami zamiast zajrzeć w dokumentację. printf zwraca długość stringa i to echo'ujesz. Co pokazuje że rows nic nie zawiera pewnie.
UszaTek
Cytat(viking @ 1.01.2022, 12:27:51 ) *
Pewnie dlatego że cały czas losowo strzelasz funkcjami zamiast zajrzeć w dokumentację. printf zwraca długość stringa i to echo'ujesz. Co pokazuje że rows nic nie zawiera pewnie.


Mam funkcję:
Kod
$sql = "INSERT INTO `stat` (TemperaturePot1,  TemperaturePot2, TemperaturePot3, TemperaturePot4, HumidityPot1, HumidityPot2, HumidityPot3, HumidityPot4, TemperatureWaterPot1, TemperatureWaterPot2, TemperatureWaterPot3, TemperatureWaterPot4, pHPot1, pHPot2, pHPot3, pHPot4) VALUES (11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26)";
        if(isset($_POST['passed_exam']))
        {
            echo validation_sql($conn, $sql);
        }
        
    while ($row = mysqli_fetch_row($result))
    {
        printf("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s", $row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8], $row[9], $row[10], $row[11], $row[12], $row[13], $row[14], $row[15]);
    }


I tutaj ładnie wyświetla dane lecz w formularzu jest cały czas " 0 "
viking
W miejscu tego wyświetlenia wstaw var_dump($rows) i pokaż wynik.
UszaTek
Teraz taki efekt;



Całość:

Kod
<form action="settings.php" method="post">
                    <label for="s1">Temperature: </label>
                        <div class="ui-spinner">
                            <input type="text" id="s1" name="TemperaturePot1" class="ui-spinner-box" autocomplete="off" value=<? echo  var_dump($rows); ?>>
                            <button class="ui-spinner-up" type="button">▲</button>
                            <button class="ui-spinner-down" type="button">▼</button>
                        </div>
                </form>
viking
Nie jako wartość tylko gdzieś przed.
UszaTek
Dodałem przed tabele:
Kod
<? echo var_dump($rows); ?>


Efekt:
Kod
array(0) { }
viking
No i sam widzisz że nie masz tam żadnego wyniku.
UszaTek
Cytat(viking @ 1.01.2022, 14:48:21 ) *
No i sam widzisz że nie masz tam żadnego wyniku.


Zobacz tak wygląda cała moja funkcja:

Kod
<?php
    $conn = mysqli_connect("myxt8.pl", "m262xst", "x$", "mxest");
    
    if($conn === false)
    {
        die("ERROR: Could not connect. " .mysqli_connect_error());
    }

    $query="SELECT * FROM stat";
    $result=mysqli_query($conn, $query);
          
    function validation_sql($c, $sql)
    {
        if(mysqli_query($c, $sql))
    {
        echo nl2br("$TemperaturePot1\n $TemperaturePot2\n $TemperaturePot3\n $TemperaturePot4\n $HumidityPot1\n $HumidityPot2\n $HumidityPot3\n $HumidityPot4\n $TemperatureWaterPot1\n $TemperatureWaterPot2\n $TemperatureWaterPot3\n $TemperatureWaterPot4\n $pHPot1\n $pHPot2\n $pHPot3\n $pHPot4");
    }
    else
    {
        echo "ERROR: Hush! Sorry $sql. "
            .mysqli_error($c);
                return 'false';
        }
    }

    $sql = "INSERT INTO `stat` (TemperaturePot1,  TemperaturePot2, TemperaturePot3, TemperaturePot4, HumidityPot1, HumidityPot2, HumidityPot3, HumidityPot4, TemperatureWaterPot1, TemperatureWaterPot2, TemperatureWaterPot3, TemperatureWaterPot4, pHPot1, pHPot2, pHPot3, pHPot4) VALUES (11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26)";
        if(isset($_POST['passed_exam']))
        {
            echo validation_sql($conn, $sql);
        }
        
    while ($row = mysqli_fetch_row($result))
    {
        printf("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s", $row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8], $row[9], $row[10], $row[11], $row[12], $row[13], $row[14], $row[15]);
    }

    $TemperaturePot1 = (int) filter_input(INPUT_POST, 'TemperaturePot1', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperaturePot1 = " . $TemperaturePot1 . " where id=1;";
        echo validation_sql($conn, $sql);
    
    $TemperaturePot2 = (int) filter_input(INPUT_POST, 'TemperaturePot2', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperaturePot2 = " . $TemperaturePot2 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $TemperaturePot3 = (int) filter_input(INPUT_POST, 'TemperaturePot3', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperaturePot3 = " . $TemperaturePot3 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $TemperaturePot4 = (int) filter_input(INPUT_POST, 'TemperaturePot4', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperaturePot4 = " . $TemperaturePot4 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $HumidityPot1 = (int) filter_input(INPUT_POST, 'HumidityPot1', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set HumidityPot1 = " . $HumidityPot1 . " where id=1;";
        echo validation_sql($conn, $sql);
    
    $HumidityPot2 = (int) filter_input(INPUT_POST, 'HumidityPot2', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set HumidityPot2 = " . $HumidityPot2 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $HumidityPot3 = (int) filter_input(INPUT_POST, 'HumidityPot3', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set HumidityPot3 = " . $HumidityPot3 . " where id=1;";
        echo validation_sql($conn, $sql);
    
    $HumidityPot4 = (int) filter_input(INPUT_POST, 'HumidityPot4', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set HumidityPot4 = " . $HumidityPot4 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $TemperatureWaterPot1 = (int) filter_input(INPUT_POST, 'TemperatureWaterPot1', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperatureWaterPot1 = " . $TemperatureWaterPot1 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $TemperatureWaterPot2 = (int) filter_input(INPUT_POST, 'TemperatureWaterPot2', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperatureWaterPot2 = " . $TemperatureWaterPot2 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $TemperatureWaterPot3 = (int) filter_input(INPUT_POST, 'TemperatureWaterPot3', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperatureWaterPot3 = " . $TemperatureWaterPot3 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $TemperatureWaterPot4 = (int) filter_input(INPUT_POST, 'TemperatureWaterPot4', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set TemperatureWaterPot3 = " . $TemperatureWaterPot3 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $pHPot1 = (int) filter_input(INPUT_POST, 'pHPot1', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set pHPot1 = " . $pHPot1 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $pHPot2 = (int) filter_input(INPUT_POST, 'pHPot2', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set pHPot2 = " . $pHPot2 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $pHPot3 = (int) filter_input(INPUT_POST, 'pHPot3', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set pHPot3 = " . $pHPot3 . " where id=1;";
        echo validation_sql($conn, $sql);
        
    $pHPot4 = (int) filter_input(INPUT_POST, 'pHPot4', FILTER_SANITIZE_NUMBER_INT);
    $sql="update stat set pHPot4 = " . $pHPot4 . " where id=1;";
        echo validation_sql($conn, $sql);

$sql = <<<SQL
SELECT
  `TemperaturePot1`,
  `TemperaturePot2`,
  `TemperaturePot3`,
  `TemperaturePot4`,
  `HumidityPot1`,
  `HumidityPot2`,
  `HumidityPot3`,
  `HumidityPot4`,
  `TemperatureWaterPot1`,
  `TemperatureWaterPot2`,
  `TemperatureWaterPot3`,
  `TemperatureWaterPot4`,
  `pHPot1`,
  `pHPot2`,
  `pHPot3`,
  `pHPot4`
  FROM `stat`
SQL;
    
$sql = <<<SQL
UPDATE `stat` SET
  `TemperaturePot1` = ?,
  `TemperaturePot2` = ?,
  `TemperaturePot3` = ?,
  `TemperaturePot4` = ?,
  `HumidityPot1` = ? ,
  `HumidityPot2` = ?,
  `HumidityPot3` = ?,
  `HumidityPot4` = ?,
  `TemperatureWaterPot1` = ?,
  `TemperatureWaterPot2` = ?,
  `TemperatureWaterPot3` = ?,
  `TemperatureWaterPot4` = ?,
  `pHPot1` = ?,
  `pHPot2` = ?,
  `pHPot3` = ?,
  `pHPot4` = ?
  LIMIT 1;
SQL;

    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param
    (
        $stmt, 'iiiiiiiiiiiiiiii', $TemperaturePot1, $TemperaturePot2, $TemperaturePot3, $TemperaturePot4, $HumidityPot1, $HumidityPot2, $HumidityPot3, $HumidityPot4, $TemperatureWaterPot1, $TemperatureWaterPot2, $TemperatureWaterPot3, $TemperatureWaterPot4, $pHPot1, $pHPot2, $pHPot3, $pHPot4
    );
    mysqli_stmt_execute($stmt);
    
    $res = mysqli_query($conn, $sql);

    while($foo = mysqli_fetch_row($res))
    {
        echo $foo[0];
    }

    $rows = mysqli_fetch_all($result, MYSQLI_NUM);    
?>
viking
Widzę sporo głupot na które już Ci zwracaliśmy uwagę. M.in. bezpośrednie nadpisanie sql. Nigdzie nie widzę przekazania wyniku do skryptu.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.