![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 20 Pomógł: 0 Dołączył: 24.04.2017 Ostrzeżenie: (0%) ![]() ![]() |
Witam, jestem w trakcie konstruowania podwalin strony dzięki której będę mógł wł/wył światło. Wymyśliłem sobie, że poprzez formularz będę sprawdzał stan wł lub wył. Stworzyłem w phpmyadmin odpowiednią bazę która zawiera następujące tabele: id - primary key, timeline - timestamp, Pin_1, Pin_2, Pin_3, Pin_4 - varchar. Stworzyłem pilik connect.php
Kod <?php $user = "root"; $pass = ""; $host = "localhost"; $connect = mysql_connect($host,$user,$pass); $selectdb = mysql_select_db('homelight', $connect); if($connect) { echo "Połączono z bazą Home Light"; } else{ echo "Brak połączenia z bazą Home Light"; } ?> plik rewrite.php Kod <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Przetowrzenie żądania</title> </head> <body> <h1>Oświetlenie</h1> <h2>Włączam oświetlenie na pinach:</h2> <?php include("connect.php"); $Pin_1 = $_POST['Pin_1']; $Pin_2 = $_POST['Pin_2']; $Pin_3 = $_POST['Pin_3']; $Pin_4 = $_POST['Pin_4']; $control = 1; $on = "On"; $off = "Off"; if($control = $Pin_1){ $Pin_1 = $on; echo 'Pin 1: ' .$Pin_1. '<br />'; } else{ $Pin_1 = $off; echo 'Pin 1: ' .$Pin_1. '<br />'; } if($control = $Pin_2){ $Pin_2 = $on; echo 'Pin 2: ' .$Pin_2. '<br />'; } else{ $Pin_2 = $off; echo 'Pin 2: ' .$Pin_2. '<br />'; } if($control = $Pin_3){ $Pin_3 = $on; echo 'Pin 3: ' .$Pin_3. '<br />'; } else{ $Pin_3 = $off; echo 'Pin 3: ' .$Pin_3. '<br />'; } if($control = $Pin_4){ $Pin_4 = $on; echo 'Pin 4: ' .$Pin_4. '<br />'; } else{ $Pin_4 = $off; echo 'Pin 4: ' .$Pin_4. '<br />'; } $sql_insert = "insert into tableoflight (Pin_1,Pin_2,Pin_3,Pin_4) values ('$Pin_1','$Pin_2','$Pin_3','$Pin_4')"; mysql_query($sql_insert); if($sql_insert) { echo "Rewrite is sucesfull"; }else{ echo "Rewrite is not connected. Error"; } ?> <a href="index.php">wstecz</a> </body> </html> no i plik index.php który będę przerabiał gdyż łączę się w nim z bazą 4 razy co jest zupełnie bez sensu Kod <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Home Light</title> </head> <body> <form action="rewrite.php" method=post> <table border="1" cellspacing="2" cellpadding="5"> <tr bgcolor="red"> <td width="150">Światło:</td> <td width="15">Przekaźnik:</td> <td width="35">Status:</td> </tr> <tr> <td>Pin 1</td> <td align="center"><input type="radio" name="Pin_1" size="3"></td> <?php include("connect.php"); //SELECT MAX(id) FROM tabelaoflight || select * from tableoflight ||mysqli_fetch_assoc $resultad = mysql_query("select * from tableoflight"); while($linha = mysql_fetch_array($resultad)) { echo '<td>'.$linha["Pin_1"]. '</td>'; } ?> </tr> <tr> <td>Pin 2</td> <td align="center"><input type="radio" name="Pin_2" size="3"></td> <?php include("connect.php"); //SELECT MAX(id) FROM tabelaoflight || select * from tableoflight $resultad = mysql_query("select * from tableoflight"); while($linha = mysql_fetch_array($resultad)) { echo '<td>'.$linha["Pin_2"]. '</td>'; } ?> </tr> <tr> <td>Pin 3</td> <td align="center"><input type="radio" name="Pin_3" size="3"></td> <?php include("connect.php"); //SELECT MAX(id) FROM tabelaoflight || select * from tableoflight $resultad = mysql_query("select * from tableoflight"); while($linha = mysql_fetch_array($resultad)) { echo '<td>'.$linha["Pin_3"]. '</td>'; } ?> </tr> <tr> <td>Pin 4</td> <td align="center"><input type="radio" name="Pin_4" size="3"></td> <?php include("connect.php"); //SELECT MAX(id) FROM tabelaoflight || select * from tableoflight $resultad = mysql_query("select * from tableoflight"); while($linha = mysql_fetch_array($resultad)) { echo '<td>'.$linha["Pin_4"]. '</td>'; } ?> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Włącz"></td> </tr> </table> </form> </body> </html> Idea: klikam w formularzu, który pin mam zamiar włączyć a to przekazuje dane do BD która zapisuje aktualny stan i po kliknięciu wstecz chce uzyskać status pinu. Wymyśliłem, aby wg. id wyświetlić stan, tylko nie wiem jak wybrać jeden najaktualniejszy rekord z BD? Czy ktoś mógłby mi podpowiedzieć jak to ogarnąć? Próbowałem poprzez zapytanie: select max(id) from tableoflight jednak zwraca mi błąd. Zaczynam się bawić PHP i MYSQL w związku z powyższym proszę o wyrozumiałaś. Pozdrawiam! |
|
|
![]() ![]() |
![]() |
Aktualny czas: 19.08.2025 - 09:14 |