0

New to PHP, first post on stackoverflow... From the tutorials I've read, I'm 'somewhat' protected from injection, and I know I'm passing my variables correctly from my search form. The issue I'm having is when I attempt to read the results; I receive an "undefined index" error for them and I have no idea why. I've been staring at this and reading for the last day and am about to pull out my hair. Would someone be able to point me in the right direction? and yes, i know, don't use root; this is purely for demo and hosted locally until i have everything smoothed out. Thanks for any help! -Jason

if(isset($_POST['submit'])){
    if(isset($_GET['go'])){
        if(preg_match("%[a-zA-Z0-9_-]%", $_POST["booktext"])){
            $searchvalue=$_POST["booktext"];

            // create connection

            $user="root";
            $password="";
            $database="bookcat_data";
            $host="127.0.0.1:3306";
            $searchtype=$_POST["searchtype"];

            $con=mysqli_connect($host, $user, $password, $database);

            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

            $sql = "SELECT Book.BookID, Book.Title, Book.AuthorSort, Publisher.Publisher, Book.PublishDate, Book.ISBN FROM ((Book LEFT JOIN Edition ON Book.EditionID = Edition.EditionID) LEFT JOIN PrintedBy ON Book.PrintedByID = PrintedBy.PrintedByID) LEFT JOIN Publisher ON Book.PublisherID = Publisher.PublisherID WHERE " . $searchtype . " LIKE '%" . $searchvalue . "%' ORDER BY Book.Title;";

            $result=mysqli_query($con,$sql);

            switch($searchtype) {
                case "Book.Title":
                    $header="Book Title";
                    break;
                case "Book.AuthorSort":
                    $header="Author";
                    break;
            }


            echo '<title>' . $header . ' Search for "' . $searchvalue . '"</title>';
            echo '<h3 align="center">' . $header . ' Search for "' . $searchvalue . '" - Sorted by Title</h3>';
            echo '<table cellpadding="3" cellspacing="2" border="1" id="catalogs" class="center">';
            echo '<tr><th>Book Title</th><th>Author</th><th>Publisher</th><th>Publish Date</th><th>ISBN</th>';

            while($row=mysqli_fetch_array($result)) {
                $BookID=$row['Book.BookID'];
                $BookTitle=$row['Book.Title'];
                $Author=$row['Book.AuthorSort'];
                $Publisher=$row['Publisher.Publisher'];
                $PublishDate=$row['Book.PublishDate'];
                $ISBN=$row['Book.ISBN'];

                echo '<tr>';
                echo '<td><a href=\'bookinfo.php?id=$BookID\'>' . $BookTitle . '</td>';
                echo '<td>' . $Author . '</td>';
                echo '<td>' . $Publisher . '</td>';
                echo '<td>' . $PublishDate . '</td>';
                echo '<td>' . $ISBN . '</td>';
                echo '</tr>';
                }


            echo '</table>';

            mysqli_free_result($result);

            mysqli_close($con);
    }
}
else{
    echo "<p>Please enter an appropriate search</p>";
}
}
4
  • A key tool for PHP debuging is using var_dump, if you use var_dump($row); you will be able to see the actual indexes used for the array and access them as appropriate. Commented May 29, 2014 at 14:11
  • 2
    Which line is causing the error? You probably need to remove the table-name part of your indexes. So instead of $row['Book.BookID'], try $row['BookID'] Commented May 29, 2014 at 14:11
  • 3
    Found 5,842 results with 'PHP - Undefined index' on SO! Commented May 29, 2014 at 14:11
  • Patrick - correct, the $row[...] variables are what's causing me the issue. Thank you so much! That's exactly it! :D Commented May 29, 2014 at 14:14

1 Answer 1

2

SQL queries return the column names without the table prefix. Remove this prefix when accessing your array elements. So $row['BookID'] instead of $row['Book.BookID'].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.