简体   繁体   中英

not able to write data in database in mysql

I have created a product insertion form in php. While clicking on insert button all values get stored in database except the category_id(cat_id). The form is working fine but only problem with the updation of category_id in the database

<form action="insert_product.php" method="post" enctype="multipart/form-data">
        <table align="center" width="700" border="2" bgcolor="orange">
            <tr align="center">
                <td colspan="8"><h2>Insert New Product</h2></td>
            </tr>
            <tr>
                <td align="center">Product Title</td>
                <td><input type="text" name="product_title" size="60"/></td>
            </tr>
            <tr>
                <td align="center">Product Category</td>
                <td>
                    <select name="product_cat">
                        <option>Select  your Category</option>
                        <?php
                            $get_cats ="select * from categories";

                            $run_cats=mysqli_query($con,$get_cats);

                            while($row_cats=mysqli_fetch_array($run_cats))
                            {
                                $cat_id = $row_cats['cat_id'];
                                $cat_title = $row_cats['cat_title'];

                                echo "<option value='$cat_id'>$cat_title</option>";
                                echo $cat_id;
                            }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="center">Product Brand</td>
                <td>
                    <select name="product_brand">
                        <option>Select your Brand</option>
                        <?php
                            $get_brands ="select * from brands";

                            $run_brands = mysqli_query($con,$get_brands);

                            while($row_brands=mysqli_fetch_array($run_brands))
                            {
                                $brand_id = $row_brands['brand_id'];
                                $brand_title = $row_brands['brand_title'];

                                echo "<option value='$brand_id'>$brand_title</option>";
                            }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="center">Product Image</td>
                <td><input type="file" name="product_image"/></td>
            </tr>
            <tr>
                <td align="center">Product Price</td>
                <td><input type="text" name="product_price"/></td>
            </tr>
            <tr>
                <td align="center">Product Description</td>
                <td><textarea name="product_desc" cols="20" rows="10"></textarea></td>
            </tr>
            <tr>
                <td align="center">Product Keywords</td>
                <td><input type="text" name="product_keywords" size="60"/></td>
            </tr>
            <tr>
                <td colspan="7" align="center"><input type="submit" name="insert_post" value="Insert Now"/></td>
            </tr>
        </table>
    </form>

Php Code

<?php
if(isset($_POST['insert_post']))
{
    $product_title = $_POST['product_title'];
    $product_cat = $_POST['product_cat'];
    $product_brand = $_POST['product_brand'];
    $product_price = $_POST['product_price'];
    $product_desc = $_POST['product_desc'];
    $product_keywords = $_POST['product_keywords'];

    $product_image=$_FILES['product_image']['name'];
    $product_image_tmp=$_FILES['product_image']['tmp_name'];

    move_uploaded_file($product_image_tmp,"product_images/$product_image");

    echo $insert_product="insert into products
    (product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keywords) values ('
    $product_cat','$product_brand','$product_title','$product_price','$product_desc','$product_image','$product_keywords')";

    $insert_pro = mysqli_query($con, $insert_product);

    if($insert_pro)
    {
        echo "<script>alert('Product inserted successfully.')</script>";
        echo "<script>window.open('insert_product.php','_self')</script>";
    }
}
?>

Database Table

create table products(
product_id int(100) auto_increment primary key,
product_cat int(100),
product_brand int(100),
product_title varchar(255),
product_price int(100),
product_desc text(200),
product_image text(100),
product_keywords text(200)
)

create table categories(
cat_id int(100) auto_increment primary key,
cat_title varchar(255)
)

create table brands(
brand_id int(100) auto_increment primary key,
brand_title varchar(255)
)

Replace the following line of your code:

echo "<option value='$cat_id'>$cat_title</option>";

with this

echo "<option value='".$cat_id."'>".$cat_title."</option>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM