Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I move code to index.php because I want it to be displayed in current divs.
When I did it this happened - code here:
<<?php session_start();
 // Check if the user is logged in, if not then redirect him to login page
 if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
     header("location: login.php");
     echo $username;
 } ?>
    if(!$searchtype || !$searchterm) {
        echo '<p>Nezadali ste niektory z udajov pre vyhladavanie.<br>
        Vratte sa prosim spat a skuste to znovu!</p>';
    switch ($searchtype) {
        case 'author';
        case 'title';
        case 'isbn';
        default: echo '<p>Zadali ste neplatny typ hladania.<br>Vratte sa prosim spat a skuste to znovu.</p>';

    @$db = new mysqli('localhost:3306','root','','books');
    if (mysqli_connect_errno()) {
        echo '<p>Chyba: Nepodarilo sa pripojit k databaze.<br>
        Kontaktujte prosim administratora.</p>';

    $query = "SELECT isbn, author, title FROM books WHERE $searchtype LIKE ?";
    $stmt = $db->prepare($query);
    $searchterm= '%'.$searchterm.'%';
    echo "<p>Pocet najdenych knih: ".$stmt->num_rows."</p>";

    while($stmt->fetch()) {
        echo "<p>Nazov: ".$title."";
        echo "<br>Autor: ".$author;
        echo "<br>ISBN: ".$isbn;

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <script src="" crossorigin="anonymous"></script>
    <link href="" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script src="" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

    <title>Pozicovna knih</title>
    <div class="container">
        <div class="item"><p><h2>Vitajte <?php if(isset($_SESSION['username'])){
    echo " '{$_SESSION['username']}'";
} ?> !</p></p></h2>
					<div class="form">
                        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
                        <p>Vyberte typ hladania:    
                        <select name="searchtype">
                                <option value="author">Autor</option>
                                <option value="title">Nazov</option>
                                <option value="isbn">ISBN</option>
                            <p>Zadajte hladany vyraz
                            <input name="searchterm" type="text" size="40"></p>
                                <input type="submit" name="submit" value="Hladat">
      <div class="item">    <form action="logout.php">
    <input type="submit" class="button button1" value="Logout" /></div>


Warning: Undefined array key "searchtype" in D:\xampp\htdocs\index.php on line 9

Warning: Undefined array key "searchterm" in D:\xampp\htdocs\index.php on line 10

Please write info about what to do with PHP to have it on the same page.

What I have tried:

Move code from results.php to index.php.
Richard MacCutchan 19-Nov-21 7:15am    
It looks like the PHP code is trying to run as soon as the page gets loaded, rather than waiting for the form post. Check with the original code to see if you missed something.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900