AJAX database table not working

im trying to get a table of information to appear from a database of animal type, animal number and age to appear from what ever farm the user chooses from a database but nothing appears after a farm is chosen and cant figure out why.

here is my front end code:

CHOOSE FARM NUMBER FOR ANIMAL INFO:

Select farm number: Farm 1 Farm 2 Farm 3 Farm 4 Farm 5

and here is my backend php:

table { width: 100%; border-collapse: collapse; }

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}

<?php $q = intval($_GET['q']); $con = mysqli_connect('localhost','root','root','farm_database'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"ajax_demo"); $sql = "SELECT animaltype, animalnumber, age FROM animal"; = '".$q."'"; $result = mysqli_query($con,$sql); echo ""; while($row = mysqli_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
animal type animal number age
" . $row['animal type'] . "" . $row['animal number'] . "" . $row[age'] . "
"; mysqli_close($con); ?>

Your query is written wrong.

Your query ends before it is finished!

$sql = “SELECT animaltype, animalnumber, age FROM animal”; = ‘".$q."’";

This ends after animal BEFORE the equal… Try

$sql = “SELECT animaltype, animalnumber, age FROM animal = '” . $q . “’”;

Better yet,

[php]
$sql = “SELECT animaltype, animalnumber, age FROM animal WHERE some_column = ?”;

if ($stmt = $mysqli->prepare( $sql )) {

$stmt->bind_param("s", $q);
$stmt->execute();

// fill in the rest
[/php]

I don’t know what you table structure is, but I don’t see how you would properly pull the data from the table. What ID links the animals to a specific farm?

Sponsor our Newsletter | Privacy Policy | Terms of Service