Last active
August 31, 2016 18:37
-
-
Save dz0/09ed63cf9a89460236ed4142ab485a3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // patikrinti ar duomenys yra ir susikurti kintamuosius $name ir $msg | |
| // jiems priskirti tuščias eilutes, jei duomenys nepateikti... | |
| // if( array_key_exists("name", $_POST) ) | |
| // $name = $_POST[ "name" ]; | |
| // else $name = null; | |
| function get_info( $key ){ | |
| if( array_key_exists($key, $_POST) ) | |
| return $_POST[ $key ]; | |
| // kitu atveju pagal nutylėjimą grąžina null -- tuščią reikšmę | |
| } | |
| $name = get_info("name"); | |
| $msg = get_info("msg"); | |
| function duomenys_faile(){ | |
| $myfile = fopen("chat.txt", "a"); //a - append | |
| // IP adresas $_SERVER['REMOTE_ADDR'] | |
| $txt = $_SERVER['REMOTE_ADDR'] . " ${GLOBALS['name']} : ${GLOBALS['msg']} <br>\n"; | |
| fwrite($myfile, $txt); | |
| fclose($myfile); | |
| $myfile = fopen("chat.txt", "r"); //r - read | |
| echo fread($myfile,filesize("chat.txt")); | |
| fclose($myfile); | |
| } | |
| function duomenys_DB(){ | |
| // prisijungimui galima naudoti msqli (senoviškesnis, bet paprastesnis) arba PDO (objektinis bei saugesnis) būdą | |
| // http://www.w3schools.com/php/php_mysql_prepared_statements.asp | |
| // daaaug info https://phpdelusions.net/pdo#query | |
| // prisijungimo reikalai | |
| $servername = "localhost"; | |
| $username = "root"; | |
| $password = ""; | |
| $dbname = "pokalbiai"; | |
| $db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // reik iškviest tik kartą programoje | |
| $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to exception | |
| try{ | |
| // užklausa su PARAMETRAIS | |
| // paruošiam užklausą nurodydami, kur įdėsim parametrus | |
| $stmt = $db->prepare("INSERT INTO `messages` (`person`, `msg`) VALUES (?, ?);"); // vietoj klaustukų įdės reikšmes | |
| // vykdom ir paduodam parametrus masyve | |
| $stmt->execute([ $GLOBALS['name'], $GLOBALS['msg'] ]); | |
| } | |
| catch(PDOException $e) { | |
| if ( $GLOBALS['name']==null && $GLOBALS['msg']==null){ } //jei | |
| else echo "Error: " . $e->getMessage(); | |
| } | |
| // PAPRASTA užklausa | |
| $stmt = $db->query('SELECT * FROM messages'); | |
| while ($row = $stmt->fetch()) // fetch duoda po vieną įrašą | |
| { | |
| $stmt2 = $db->prepare('SELECT * FROM persons WHERE name=?'); | |
| $stmt2->execute([ $row['person'] ]); | |
| $person = $stmt2->fetch(); | |
| echo $row['person']. " (${person['age']}) : ". $row['msg'] . "<br>\n"; | |
| } | |
| } | |
| //duomenys_faile(); // iškviečiam | |
| duomenys_DB(); | |
| ?> | |
| <form action="chat.php" method="post"> | |
| Name: <input type="text" name="name" value="<?php echo $name;?>"><br> | |
| Message: <input type="text" name="msg"><br> | |
| <input type="submit"> | |
| </form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment