<?php
        connection check;
        select database;
        $sql="select * from category"
        mysql_query($sql);
        $prod_name=$_POST["prod_name"];

/pls tell me what to pass in cat_id down i have to take the select tag's value property then what should i pass pls help me........./
$cat_id="What to write here...."
sql1="insert into product(prod_name,cat_id) values('".$prod_name."','".$cat_id."');
$r=mysql_query($sql1);
?>

        <html>
            <form>
                <select id=prod name=prod>
                    <?php
                        while ($row=mysql_fetch_array($r))
                        {
                            echo "<option value=".$row["cat_id"].">$row["cat_name"] </option>";   
                        }
                        //can you tell me how to add the above value of cat_id and insert to table....
                    ?>
                </select>
            </form>
        </html>

Dani AI

Generated

A few clarifications and a safe, practical workflow to finish what started (and to expand on ’s hint):

Give the select a clear name (for example cat_id) and include a submit button. Do not try to INSERT while you are looping to build the <select>; build the list first, then submit the form and perform the INSERT on the action page. The action page should validate the incoming values, confirm the category actually exists (optional but recommended), and then insert.

Use prepared statements (PDO or mysqli) rather than the old mysql_* functions to avoid SQL injection and PHP compatibility problems. Validate that the product name is not empty and that the category id is an integer before running the insert. Also enable error reporting or use try/catch so you can see why an insert might fail.

Example (processing logic, simplified):

<?php
// assume $pdo is a connected PDO instance
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $prodName = trim($_POST['prod_name'] ?? '');
    $catId = $_POST['cat_id'] ?? '';

    if ($prodName === '' || !ctype_digit($catId)) {
        // handle invalid input
    } else {
        $stmt = $pdo->prepare('INSERT INTO product (prod_name, cat_id) VALUES (:prod, :cat)');
        $stmt->execute([':prod' => $prodName, ':cat' => (int)$catId]);
    }
}
?>

Troubleshooting tips: confirm the form uses method="post" and the select’s name matches what you read on the server; check DB connection and SQL errors; verify the insert page is the form’s action; and prefer PDO/mysqli for modern PHP.

Recommended Answers

All 4 Replies

When will you want to insert cat_id ? When the form has submitted ? You must have action to process server-side script. So, put action in your form via "action=somewhere" attribute. Somewhere could be server-side language php / jsp / asp file. Then, you can get the data in this page submitted by form and insert into database table with specific language (PHP-MySQL, ASP-MsSQL, etc.).

        <html>
            <form action="allprod.php" method="post">
                <select id=prod name=prod>
                    <?php
                        while ($row=mysql_fetch_array($r))
                        {
                            echo "<option value=".$row["cat_id"].">$row["cat_name"] </option>";   
                        }
                        //can you tell me how to add the above value of cat_id and insert to table....
                    ?>
                </select>
            </form>
        </html>

now pls tell me that should i pass $row["cat_id"] as a cat_id or some thing else which is selected.....

You can now get 'cat_id' in allprod.php by using $_POST['prod'], and then, go with your SQL query to insert that value into table.

so i should insert after the while loop and fire the sql query.....

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.