Reputation: 19
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>";
}
}
Upvotes: 0
Views: 649
Reputation: 6393
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']
.
Upvotes: 2