I´m a bloody beginner and I would be more than happy, if someone could help me with my code. I have an SQL-database with columns containing a variable number of strings and columns containing numbers, all separated by commas. Now, I want to read out data by using a html-form and php. Each user can decide himself, which information to read out, that means, there are no mandatory fields. The result should be the fruit that meets all requested criteria. Here is the code that works well.
enter code here
<?php
include("../files/zugriff.inc.php");
if (isset($_POST["submit"])) {
$sent = $_POST['sent'];
$daten = array();
$fruits = $_POST["fruits "];
$vegetables = $_POST["vegetables"];
$country = $_POST["country"];
$season = $_POST["season"];
$diameter = $_POST["diameter"];
$color = $_POST["color"];
if(!empty($fruits)) {
$daten['fruits'] = $fruits;
}
if(!empty($vegetables)) {
$daten['vegetables'] = $vegetables;
}
if(!empty($country)) {
$daten['country'] = $country;
}
if(!empty($season)) {
$daten['season'] = $season;
}
if(!empty($diameter)) {
$daten['diameter'] = $diameter;
}
if(!empty($color)) {
$daten['color '] = $color;
}
foreach ($daten as $key => $value) {
$spalten[] = $key;
$werte[] = "'$value'";
}
echo "<b>Results:</b><br><br>";
$sql = "SELECT * FROM fruitdatabase WHERE (" . implode(", ",
$spalten) . ") = (" . implode(", ", $werte) . ")";
echo "<table>";
echo "<tr><th>Names</th></tr>";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>$row[name]</td></tr>";
}
echo "</table>";
}
mysqli_close($db);
?>
However, the problem is, when there are different values separeted by commas in one field, the requested value is not found. This only works, when there is only one value in a field. So I tried to Change my code as follows:
foreach ($daten as $key => $value) {
$spalten[] = $key;
$werte[] = "'%$value%'";
}
echo "<b>Results:</b><br><br>";
$sql = "SELECT * FROM fruitdatabase WHERE (" . implode(", ",
$spalten) . ") LIKE (" . implode(", ", $werte) . ")";
Now, single words, separated by commas, are found. However, the combination of different requests is no more possible, which means that I can only search for one criterion and not for more.
I think there must be a problem with the shortcut of the array-elements.
Many thanks! Gery