将数据从db加载到html下拉列表中,并使用下拉列表中的数据再次搜索数据库

时间:2017-09-21 23:45:50

标签: php mysql dropdown

我在db中有一些数据要加载到html下拉列表,然后在下拉列表中使用该文本再次搜索db,但我无法这样做。问题是我有1个表单,但有2个提交按钮(因为我无法从html按钮调用php函数,大多数代码在线,他们只是不工作)。我可以获取数据并填充下拉列表但我无法使用下拉列表中的文本再次搜索数据库,我收到一个未定义的索引错误(请注意下拉列表是php生成的),任何提示,谢谢。< / p>

<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>

0 个答案:

没有答案