Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. $host = "localhost"; // use your real host name
  5. $username = "roton";   // use your real login user name
  6. $password = "Roton123";   // use your real login password
  7. $dbName = "rotonhageton"; // use your real database name
  8.  
  9. $con = mysqli_connect( "$host", "$username", "$password", "$dbName" );
  10.  
  11. if( !$con ) // == null if creation of connection object failed
  12. {
  13.     // report the error to the user, then exit program
  14.     die("connection object not created: ".mysqli_error($con));
  15. }
  16.  
  17. if( mysqli_connect_errno() )  // returns false if no error occurred
  18. {
  19.     // report the error to the user, then exit program
  20.     die("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
  21. }
  22.  
  23.  
  24. $login = "krisu";
  25. $password = "krisu123";
  26.  
  27. if($_POST['loginForm']) {
  28.     if($_POST['password'] == $password && $_POST['login'] == $login) {
  29.         $_SESSION['loggedIn'] = true;
  30.     } else {
  31.         $_SESSION['loggedIn'] = false;
  32.     }
  33. }
  34.  
  35. if($_POST['addBookForm']) {
  36.     $sql = "INSERT INTO 'books' ('book_author', 'book_name', 'price') VALUES (".$_POST['book_author'].", ".$_POST['book_name'].", ".$_POST['book_price'].")";
  37.     if ($con->query($sql) === TRUE) {
  38.     echo "New record created successfully";
  39.     } else {
  40.         echo "Error: " . $sql . "<br>" . $con->error;
  41.     }
  42. }
  43.  
  44. if($_SESSION['loggedIn'] == false) {
  45. ?>
  46.  
  47. <form method="post">
  48.     Login:
  49.     <input type="text" name="login"></input><br>
  50.     Password:
  51.     <input type="password" name="password"></input><br>
  52.     <input type="submit" name="loginForm"></input>
  53. </form>
  54. <?php
  55. } else {
  56. ?>
  57. <form method="post">
  58.     Nowa ksiazka:
  59.     <input type="text" name="book_name"></input><br>
  60.     Autor:
  61.     <input type="text" name="book_author"></input><br>
  62.     Cena:
  63.     <input type="number" name="book_price"></input>zł<br>
  64.     <input type="submit" name="addBookForm">
  65. </form>
  66. <?php
  67. if ($result = $con->query("SELECT * FROM 'books'")) {
  68.     while ($row=mysqli_fetch_assoc($result))
  69.       {
  70.           echo "Ksiazka".$row['book_title']."ktorej autor to ".$row['book_author']." kosztuje ".$row['price']."<BR>";
  71.       }
  72.     $result->close();
  73. }
  74.  
  75. }
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement