当我按下提交按钮时,尝试每件事但仍然在Xamp中找不到OBJECT NOT FOUND错误?

时间:2015-05-26 23:12:50

标签: javascript php html mysql

我正在关注 PHP和MySQL中的电子商务网站的一些教程。 但是当我点击提交按钮时,我得到 OBJECT NOT FOUND ERROR。

以下是代码,请指导我。目录等都很好。我也尝试将该按钮括在单独的表单标签中,但没有任何效果。

<?php
include("includes/db.php");

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Products</title>
</head>

<body bgcolor="#3B6AD8">
<form method="post" action="insert_product" enctype="multipart/form-data">
    <table width="700" height="650" align="center" border="2" bgcolor="#F3F3F3">
        <tr>
            <td colspan="2">
                <h2 align="center">Insert Product Here</h2>
            </td>
        </tr>
                <tr>
                <td><b>Product Title</b></td>
                <td><input type="text" name="product_title" /></td>

        </tr>

        <tr>
        <td><b>Product Category</b></td>
            <td>
                <select name="product_cat">
                <option>Select a Category</option>
                 <?php
                    $get_categories = "SELECT * FROM categories";
                    $run_categories = mysqli_query($con,$get_categories);

                    while($row_categories =mysqli_fetch_array($run_categories) ) {

                        $cat_id = $row_categories['cat_ID'];
                        $cat_title = $row_categories ['cat_title'];                     

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


                        }

                       ?>
                       </td>
                </select>
           <tr>
                <td><b>Product Brand</b></td>
                <td><select name="product_brand">
                <option>Select a Category</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>";


                        }

                       ?>
                       </td>
                </select>

            </td>

        </tr>


        <tr>
        <td><b>Product Image 1</b></td>
            <td>
                <input type="file" name="product_img1" />
            </td>

        </tr>

        <tr>
        <td><b>Product Image 2</b></td>
            <td>
                <input type="file" name="product_img2" />
            </td>

        </tr>

        <tr>
        <td><b>Product Image 3</b></td>
            <td>
                <input type="file" name="product_img3" />
            </td>

        </tr>

        <tr>
        <td><b>Product Price</b></td>
            <td>
                <input type="text" name="product_price" />
            </td>

        </tr>

        <tr>
        <td><b>Product Description</b></td>
            <td>
                <textarea name="product_desc" cols="30" rows="5"></textarea>
            </td>

        </tr>

        <tr>
        <td>Product Keywords</td>
            <td>
                <input type="text" name="product_keywords" />
            </td>
        </tr>


        <tr align="center">
            <td colspan="2">
                <form method="post">
                <input type="submit" name="add_product" value="Add Product"/>
                </form>
            </td>

        </tr>
        </table>
        </form>

</body>
</html>

<?php

    if(isset($_POST['add_product'])){

        //text data variables
        $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_status = 'on';

        //Produt Images
        $product_img1 = $_FILES['product_img1']['name'];
        $product_img2 = $_FILES['product_img2']['name'];
        $product_img3 = $_FILES['product_img3']['name'];

        //Image Temp Names
        $temp_name1 = $_FILES['product_img1']['tmp_name'];
        $temp_name2 = $_FILES['product_img2']['tmp_name'];
        $temp_name3 =  $_FILES['product_img3']['tmp_name'];


        if($product_title=="" OR $product_cat=="" OR $product_brand=="" OR $product_price=="" OR $product_desc=="" OR                      $product_keywords=="" OR $product_img1==""){
            echo "<script>alert('Please Fill All The Fields')</script>";
            exit();
            }

            else {
                move_uploaded_file($temp_name1,"product_images/product_img1");
                move_uploaded_file($temp_name2,"product_images/product_img2");
                move_uploaded_file($temp_name3,"product_images/product_img3");

                $insert_product_query = "INSERT INTO products(category_ID,brand_ID,date,product_title,product_img1,
                product_img2,product_img3,product_price,product_desc,product_status) 
                values ('$product_cat','$brand_id',
                NOW,'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc',
                '$product_status')";

                $run_products = mysqli_query($con,$insert_product_query);

                if ($run_products){

                    /*echo "<script>alert('Product inserted successfully')</script>";*/


                }


            }

    }
?>

2 个答案:

答案 0 :(得分:1)

尝试对你的表格动作加一个扩展名=“insert_product”...比如.php或.asp ..另外,将你的标题改为更像“在表单提交时找不到对象”。

试试这个:

<form method="post" enctype="multipart/form-data">

答案 1 :(得分:0)

Ther有两个问题: -

  1. 问题是你写form只包含提交按钮。尝试在按钮之前删除<form method="post">

  2. 您在同一页面中编写了所有代码,但在表单中,您的操作是insert_product,其中没有.php这样的扩展名。这是完全错误的 所以尝试做这两个中的任何一个。

    a)或者定义它action= insert_product.php并创建一个具有该名称的文件,并将最后一个表单处理代码放在该文件中。

    b)或删除表单action属性并写为<form method="post" enctype="multipart/form-data">

  3. 一次尝试1点和2点b)点。并检查一次。