Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. <?php
  2. $host = "192.168.2.30";
  3. $name = "root";
  4. $pass = "root";
  5. $db = "main";
  6. ?>
  7. <html>
  8. <head>
  9. </head>
  10. <body>
  11. <h1>My cool site</h1>
  12. <p>This is server: <?php echo $_SERVER['SERVER_ADDR']; ?></p>
  13. <form method="post" action="">
  14. <input id="name" name="name" type="text" required="required" placeholder="Name">
  15. <input id="countrycode" name="countrycode" type="text" required="required" placeholder="CountryCode">
  16. <input id="Ssubmit" name="submit" type="submit" value="Send">
  17. </form>
  18. </body>
  19. </html>
  20. <?php
  21. if(isset($_POST['submit'])){
  22. if(!isset($_POST['name']) || !isset($_POST['countrycode'])){
  23. die("You didn't filled everything in. >:C");
  24. } else {
  25. $nameSQL = strip_tags($_POST['name']);
  26. $countrycodeSQL = strip_tags($_POST['countrycode']);
  27. $nameSQL = preg_replace('/[^a-zA-Z]/','', $nameSQL);
  28. $countrycodeSQL = preg_replace('/[^a-zA-Z]/','', $countrycodeSQL);
  29.  
  30. if(strlen($nameSQL) < 1 || strlen($countrycodeSQL) < 1 || strlen($nameSQL) > 35 || strlen($countrycodeSQL) > 3){
  31. die("Only valid text allowed. Name max 35, CountryCode max 3 chars. >:C");
  32. } else {
  33. try {
  34. $mysqli = new mysqli($host, $name, $pass, $db);
  35. if(!$mysqli || $mysqli->connect_error) { die("Oops, no database"); } else {
  36. $sql = "INSERT INTO city(Name, CountryCode) VALUES('".$nameSQL."','".$countrycodeSQL."')";
  37. if($mysqli->query($sql) === TRUE){ echo "New record is added"; } else { echo "Error: " . $mysqli->error; }
  38.  
  39. $mysqli->close();
  40.  
  41.  
  42. }
  43. } catch(PDOException $e){ echo "Failed".$e->getMessage(); }
  44. }
  45. }
  46. }
  47.  
  48. $mysqli = new mysqli($host, $name, $pass, $db);
  49. if(!$mysqli || $mysqli->connect_error) { die("Oops, no database"); } else {
  50. $search = "SELECT Name, CountryCode FROM city";
  51. $result = $mysqli->query($search);
  52. if($result->num_rows > 0){
  53. while($row = $result->fetch_assoc()){
  54. echo "Name: ".$row['Name']." - CountryCode: ".$row['CountryCode']."<br>";
  55. }
  56. }
  57. $mysqli->close();
  58. }
  59.  
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement