Hello,

I have created a select product using a different way like frst we select product from dropdown and click on next button on next page the dropdown have to show the company names i.e only that companies which belongs to the selected product but i am bit confused how it will work i tried to use v_id like company id but its not working can anyone help me out.

Database structure

CREATE TABLE IF NOT EXISTS `products` (
  `prod_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_name` varchar(255) NOT NULL,
  `product_price` int(20) NOT NULL,
  `prod_details` text NOT NULL,
  `v_id` int(11) NOT NULL,
  `v_name` varchar(45) NOT NULL,
  PRIMARY KEY (`prod_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`prod_id`, `product_name`, `product_price`, `prod_details`, `v_id`, `v_name`) VALUES
(6, 'Samsung Elite Book', 1200, 'Samsung Ci3 Laptop with 8GM ram 2 years official international warranty ', 3, 'Samsung'),
(8, 'Samsung Galaxy S5', 400, 'This is anew just launched product having great features then ever buy now and get your self with a valuable product', 3, 'Samsung'),
(9, 'Samsung Galaxy S4', 350, 'Samsung Galaxy S4 this product is one of the most using cellphone in the market', 3, 'Samsung'),
(10, 'LED', 0, 'wewewewe', 3, 'Samsung'),
(11, 'TV ', 500, 'ajksdjadkaskdbkasdbkasdklkasdkbkasdksbadklbaskdbjksad', 6, 'Azeem'),
(12, 'Iphone', 500, 'gasdhasgdghasdgha', 7, 'Noman'),
(13, 'Air Condintion', 600, 'This is new product in market now', 3, 'Samsung');

First page when you try to select the product

<form method="POST" action="select_vendor.php?id=<?php echo $product_id; ?>">
            <div id="order">
                <?php
                    $query      = "SELECT * FROM products";
                    $query_confirm  = mysqli_query($connection, $query);

                    echo "<select name='product'>";
                    echo "<option>" . "Select Product" . "</option>";
                    while ($row_product = mysqli_fetch_assoc($query_confirm)) {
                        $product_id = $row_product["v_id"];
                        $product = $row_product["v_name"];
                        echo "<option>" . $product . "</option>";
                    }

                    echo "</select>";
                ?>

                <input type="submit" value="NEXT >" class="btn-green" name="submit" />
            </div>
            </form>

Now this is the second page where you wil be seleting the company of your choice

<?php
                if (isset($_POST['submit'])) {
                    $product_info = $_POST['product'];
                }
                $id = $_GET['id'];
                echo "Your Select Product" . " " .$product_info;
                echo "<br />";
                echo "<a href='index.php'>Cancel</a>";

            ?>


            <div id="order">
                <?php
                    $query      = "SELECT * FROM products WHERE v_id={$id} LIMIT 1";
                    $query_confirm  = mysqli_query($connection, $query);

                    echo "<select name='product'>";
                    echo "<option>" . "Select Vendor" . "</option>";
                    while ($row_product = mysqli_fetch_assoc($query_confirm)) {
                        $product_vendor = $row_product["v_name"];
                        echo "<option>" . $product_vendor . "</option>";
                    }

                    echo "</select>";
                ?>

                <input type="submit" value="VIEW PRODUCT >" class="btn-green" />
            </div>

I am also not getting the link as well on the action if I had get the link i beleive it would be easy enough to to do so

Thank You in advance

As i understand your code. i did it like below. hope it helps you.

<form method="POST" action="select_vendor.php">

    <div id="order">
        <?php
        $query = "SELECT * FROM products";
        $con = mysqli_connect("localhost", "root", "123123", "TEST");
        $query_confirm = mysqli_query($con, $query);


        echo "<select name='product' id='product'>";
        echo "<option>" . "Select Product" . "</option>";
        while ($row_product = mysqli_fetch_assoc($query_confirm)) {
            $product_id = $row_product["v_id"];
            $product = $row_product["v_name"];
            echo "<option value='" . $product . '_' . $product_id . "'>" . $product . "</option>";
        }

        echo "</select>";
        ?>
        <input type="submit" value="NEXT >" class="btn-green" name="submit" />
    </div>

</form>


// select_vendor.php
<?php
if (isset($_POST['submit'])) {
    $product_info = $_POST['product'];
    if($product_info != ''){
       $products= explode('_',$product_info);
       $product_name=$products[0];
       $product_id=$products[1];
    }

}
echo "Your Select Product" . " " . $product_name;
echo "<br />";
echo "<a href='index.php'>Cancel</a>";
?>


<div id="order">
    <?php
    $query = "SELECT * FROM products WHERE v_id={$product_id} LIMIT 1";

    $con = mysqli_connect("localhost", "root", "123123", "TEST");
    $query_confirm = mysqli_query($con, $query);


    echo "<select name='product'>";
    echo "<option>" . "Select Vendor" . "</option>";
    while ($row_product = mysqli_fetch_assoc($query_confirm)) {
        $product_vendor = $row_product["v_name"];
        echo "<option>" . $product_vendor . "</option>";
    }

    echo "</select>";
    ?>

    <input type="submit" value="VIEW PRODUCT >" class="btn-green" />
</div>
Member Avatar for diafol

I started replying to this, then I realised it didn't make much sense.

You are retrieving the product dfrom the first dropdown, but are sending something unknown in the form action querystring. You also have the form method set to POST. This is wrong, as you're only retrieving and not using any sensitive data like passwords - you should use GET.

Additionally, the first form does not have values in the select dropdown, only text. The whole point of a select (IMO) is that you select a unique value and pass it on to something else. The value in question should be the item ID not the item NAME.

So for me, the first page would look something like this:

<?php
//connection_details

function get_options($resource,$valueField,$textField)
{
    $output = '';
    foreach($r = mysqli_fetch_array($resource))
    {
        $output .= "<output value='{$r[$valueField]}'>{$r[$textField]}</output>\n";
    }
    return $output;
}


$qc = mysqli_query($connection, "SELECT v_id, v_name FROM products");

?>

<form method="GET" action="select_vendor.php">
    <div id="order">
        <select>
            <option value=''>Select Product</option>
            <?= get_options( $qc, 'v_id', 'v_name' ) ?>
        </select>
        <input type="submit" value="NEXT >" class="btn-green" name="submit" />
    </div>
</form>

This has the advantage of passing the correct id value in the url querystring.
Your second page make little sense as you seem to be retrieving data from the SAME DB TABLE ('products'). If all your records are in one table, then there will only be one company/vendor for each product anyway, which is a pointless waste of time.

Is this a mistake or are you a bit lost with regard to setting up your database?

Got it worked tahnks all it really helped me out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.