Hi, I would be grateful for any help with the following:
I’m trying to query mysql database with a variable from an input box and return the data that matches the query.
The code I have returns the following syntax error:
ERROR: Could not able to execute SELECT Col 3 FROM TABLE 2 WHERE Col 3 = ‘COS1’ . You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘3 FROM TABLE 2 WHERE Col 3 = ‘COS1’’ at line 1
COS1 is the variable from the input box.
<form action="helpp.php" method="post">
Supplier: <input type="text" name="Animal"/> </br>
<br> </br>
<input type="submit" value="submit"/>
</form>
$link = mysqli_connect("localhost", "*****", "****", "********");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$Animal=$_POST['Animal'];
$sql = "SELECT Col 3 FROM TABLE 2 WHERE Col 3 = '$Animal' ";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table border='1'>";
echo "<th>Type</th>";
echo "<th>Breed</th>";
echo "<th>NaMe</th>";
echo "<th>Dog</th>";
echo "<th>Cat</th>";
echo "<th>Bird</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['COL 1'] . "</td>";
echo "<td>" . $row['COL 2'] . "</td>";
echo "<td>" . $row['COL 4'] . "</td>";
echo "<td>" . $row['COL 5'] . "</td>";
echo "<td>" . $row['COL 6'] . "</td>";
echo "<td>" . $row['COL 7'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
Thank you for taking the time to read this and for any advice given.
Update: The code seems to be working now. I’m guessing that the server was very slow to update the files as I made changes. Thank you for the helpful suggestions.