无法在mysql中的数据库中写入数据

时间:2016-07-02 20:24:45

标签: php html mysql sql

我在php中创建了一个产品插入表单。单击insert按钮时,除category_id(cat_id)外,所有值都存储在数据库中。表单工作正常但只有数据库中category_id更新的问题

<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>";
    }
}
?>

数据库表

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

1 个答案:

答案 0 :(得分:-1)

替换代码的以下行:

foo.domain.com
bar.domain.com

用这个

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