I am currently creating a database for an upload system - I am using a select box to limit the number of categories added to the database.
<select name="category">
<?php
$conn = mysqli_connect("localhost", "blah", "blah") or die ("No connection");
mysqli_select_db($conn, "UPLOAD") or die("db will not open");
$query = "SELECT category FROM details GROUP BY category";
$result = mysqli_query($conn, $query) or die("Invalid query");
while($row = mysqli_fetch_array($result)) {
echo "<option value=\"" . $row[0] . "\">" . $row[0] . "</option>";
}
mysqli_close($conn);
?>
</select>
<select name="reaction">
<?php
$conn = mysqli_connect("localhost", "blah", "blah") or die ("No connection");
mysqli_select_db($conn, "UPLOAD") or die("db will not open");
$query = "SELECT reaction FROM details GROUP BY reaction";
$result = mysqli_query($conn, $query) or die("Invalid query");
while($row = mysqli_fetch_array($result)) {
echo "<option value=\"" . $row[0] . "\">" . $row[0] . "</option>";
}
mysqli_close($conn);
?>
While the two queries are the same - Only the Category (The top query) is stopping form repeating multiple data from my db. When I look into my database - This is what I am finding (Which I have a hunch may be the problem).
INSERT INTO `details` (`name`, `category`, `reaction`, `photo`, `date_added`) VALUES
('Mase Laughing', 'Funny', 'Laugh
', 'mase-laugh.gif', '2013-05-01 07:16:26'),
('Movie Wink', 'Cheeky', 'Wink
', 'movie-wink.gif', '2013-05-02 12:33:12'),
('Tarzan Giggle', 'Funny', 'Wink
', 'tarzan-laugh.gif', '2013-05-02 01:33:00');
Beside the reactions I am getting these strange letters - Anyone have any Ideas? Thanks in advance.
These are "Carriage Return" () and "Newline" ( ) characters. These are commands telling your computer to go the end of the line and to a new line.
To stop your page from showing duplicate categories, change this:
$query = "SELECT category FROM details GROUP BY category";
into this:
$query = "SELECT distinct category FROM details GROUP BY category";
It will not prevent your database from storing duplicate categories (which you do no want, otherwise you would only be able to save 1 (one) line of details per categorie, but it will prevent your dropdown (select) from showing the duplicates. So, with the details you showed, it will give you a dropdown with the values 'Funny' and 'Cheecky', both of them showing only once. Add "distinct" to your other SQL statement too to have the reactions de-duplicated too:
$query = "SELECT DISTINCT reaction FROM details GROUP BY reaction";
Here I am updating original question by reducing redundant code
<?php
$conn = mysqli_connect("localhost", "root", "") or die ("No connection");
mysqli_select_db($conn, "test") or die("db will not open");
?>
<select name="category">
<?php
$query = "SELECT category FROM details GROUP BY category";
$result = mysqli_query($conn, $query) or die("Invalid query");
while($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row[0] . "'>" . $row[0] . "</option>";
}
?>
</select>
<select name="reaction">
<?php
$query = "SELECT reaction FROM details GROUP BY reaction";
$result = mysqli_query($conn, $query) or die("Invalid query");
while($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row[0] . "'>" . $row[0] . "</option>";
}
mysqli_close($conn);
?>
</select>