Reputation: 127
I am creating a php edit form. I want to show the category selected within the notebook table in a dropdown, but also show the options available in the category table so if the category needs to be changed there are option to select. Both tables are in the same database - notes. I have tried using Join Table but that did not work. Is there a way to show the current selection in the "main table" in my case the notebook table while showing the options from the category table in the dropdown?
The below displays the current selection within the dropdown.
<?php include 'dbconnections/categoryform_db.php'; ?>
<?php
//query
echo "<select name='category'>";
echo '<option value="">Category</option>';
while ($row = $result->fetch_assoc()) {
unset($id, $category);
$id = $row['id'];
$category = $row['category'];
echo '<option value="'.$category.'">'.$category.'</option>';
}
echo "</select>";
mysqli_close($conn);
?>
The above include file is the following - this will display the current selection.
<?php
$id = $_GET['id'];
$category = $_GET['category'];
$connection = new mysqli('mysql', 'username', 'psswrd#', 'dbname');
if ($connection->connect_errno > 0) {
die ('Unable to connect to database [' . $connection->connect_error . ']');
}
$sql = "SELECT id, category
FROM notebook
WHERE id=$id";
if (!$result = $connection->query($sql)) {
die ('There was an error running query[' . $connection->error . ']');
}
?>
Is there away to combine the above with the below which provides the full list of the category options into one file?
<?php
$category = $_GET['category'];
$connection = new mysqli('mysql', 'username', 'psswrd#', 'dbname');
if ($connection->connect_errno > 0) {
die ('Unable to connect to database [' . $connection->connect_error . ']');
}
$sql = "SELECT id, category
FROM category";
if (!$result = $connection->query($sql)) {
die ('There was an error running query[' . $connection->error . ']');
}
?>
Thank you in advance for any assistance on this issue.
Upvotes: 0
Views: 36
Reputation: 403
What stops you from using UNION :
SELECT id, category, 'cat' as type FROM category
UNION
SELECT id, category, 'note' as type FROM notebook WHERE id=$id
I have introduced a static text as 'type' to determine which data was fetched from which table
Upvotes: 1