I have some data in db that I want to load into an html dropdown and then use that text in dropdown to search db again, but Im not able to do so. The issue is that I have 1 form but 2 submit buttons (as I am not able to call a php function from html button, most of the codes online, they just dont work). I can fetch the data and populate the dropdown list but I am not able to use the text in the dropdown to search db again, I receive an undefined index error (please note that the dropdown is php generated), any hints, thanks.
<form method="post" action="">
<label>ENTER DATA TO ADD AND SEARCH</label><br><br>
<form id="newdata" method="post" action="">
<label>ENTER MAKE : </label></td><td><input type="text" id="make" name="makev" /><br><br>
<label>ENTER MODEL : </label></td><td><input type="text" id="model" name="model" /><br><br>
<label>ENTER PRICE : </label></td><td><input type="text" id="price" name="price" /><br><br>
<label>ENTER QUANTITY : </label></td><td><input type="text" id="quantity" name="quantity" /><br><br>
</td><td><input type="submit" name="addv" value="ADD DATA"/> <INPUT TYPE="submit" name="search" value="SEARCH DATA" /><br><br>
</form>
<?php
function loaddata()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lab4";
$conn = mysqli_connect($servername, $username, $password, $dbname);
echo '<select name="selectd">';
$query = "select distinct make from inventory";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result))
{
echo '<option>' . $row['make'] . '</option>';
}
echo '</select>';
}
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lab4";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
if (isset ($_POST["makev"]) && isset ($_POST["model"]) && isset ($_POST["price"]) && isset ($_POST["quantity"]))
{
$makev = $_POST["makev"];
$model = $_POST["model"];
$price = $_POST["price"];
$quantity = $_POST["quantity"];
if (!empty($makev) || !empty($model) || !empty($price) || !empty($quantity))
{
$query = "insert into inventory (make, model, price, quantity) values ('$makev', '$model', '$price', '$quantity');";
$result = mysqli_query($conn, $query);
echo 'NEW MAKE ADDED AND UPDATED<br><br>';
loaddata();
}
else
{
echo "EMPTY FIELDS FOUND, CANNOT ADD DATA";
}
}
if (isset($_POST['search']))
{
$makev = $_POST['selectd'];
echo $makev;
$query = "SELECT * FROM inventory where make='$makev'";
$result = mysqli_query($conn, $query);
echo '<br><br><table style="border-collapse:separate;border:1px solid silver;width:100%"><tr><th style="border-collapse:separate;border:1px solid silver;">Make</th><th style="border-collapse:separate;border:1px solid silver;">Model</th><th style="border-collapse:separate;border:1px solid silver;">Price</th><th style="border-collapse:separate;border:1px solid silver;">Quantity</th></tr>';
while($row = mysqli_fetch_assoc($result))
{
echo '<tr><td style="border-collapse:separate;border:1px solid silver;">' . $row['make'] . '</td><td style="border-collapse:separate;border:1px solid silver;">' . $row['model'] . '</td><td style="border-collapse:separate;border:1px solid silver;">' . $row['price'] . '</td><td style="border-collapse:separate;border:1px solid silver;">' . $row['quantity'] . '</td></tr>';
}
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>