-1

我在制作 PHP 脚本时遇到了困难。此代码处理应将产品添加到数据库的表单。

我试图在我的代码中寻找可能的问题,但我完全迷失了。我尝试重写我的代码,但由于某种原因,我似乎没有任何工作。我肯定错过了一些东西。

代码如下。

    <form action="" method="post">
        <table width="436" border="0px">
            <tr class="even">
                <td width="132"><label for="title">title</label></td>
                <td width="333"><input type="text" size="40px" name="title" value=""  /></td>
            </tr>
            <tr class="odd">
                <td><label style=" text-align:top" for="icon_description">short description</label></td>
                <td><textarea name="icon_description" rows="2" cols="30"></textarea></td>
            </tr>
            <tr class="even">
                <td height="95"><label for="full_description">full description</label></td>
                <td><textarea name="full_description" cols="30" rows="6"></textarea></td>
            </tr>
            <tr class="odd">
                <td><label for="category">category</label></td>
                <td><select name="category" >
                    <?php
                        foreach(get_categories() as $category)
                        {
                    ?>
                        <option value="<?php echo $category['id']; ?>"> <?php echo $category['category_name']; ?> </option>
                    <?php
                        }
                    ?>
                </select></td>
            </tr>
            <tr class="even">
                <td><label for="price">price (<em>BsF</em>)</label></td><td><textarea name="price" rows="1" cols="20"></textarea></td>
            </tr>
            <tr class="odd">
                <td><label for="stock">stock</label></td><td><textarea name="stock" rows="1" cols="20"></textarea></td>
            </tr>
            <tr class="even">
                <td></td>
                <td style="margin-top:30px"><p style="text-align:left; margin-left:20px;"><input type="submit" value="Add Product" /></p></td>
            </tr>
        </table>
    </form>
</div>

在同一页面上,在文档的顶部,我有这段代码来处理来自表单的数据并将其发送到将其添加到数据库的实际函数:

<?php
    include_once('init.php');
    include_once('store.php');

    $products = get_all_available_products();

    if( isset($_POST['title'], $_POST['icon_description'], $_POST['category'], $_POST['price'], $_POST['stock']) )
    {
        $title = $_POST['title'];
        $icon_description = $_POST['icon_description'];
        $category = $_POST['category'];
        $price = $_POST['price'];
        $stock = $_POST['stock'];

        $errors = array();

        if( empty($title) )
        {
            $errors[] = 'You need to add a title.';
        }
        else if( strlen($title) > 125 )
        {
            $errors[] = 'Title cannot be longer than 255 characters.';
        }
        if( empty($icon_description))
        {
            $errors[] = 'Short description is empty.';
        }
        if( empty($price))
        {
            $errors[] = 'You need to specify a price.';
        }
        if( empty($stock))
        {
            $errors[] = 'You need to ad stock amount.';
        }

        add_product($title, $category, $icon_description, $price, $stock);

        header('Location: products.php');
        die();
    }

add_product() 函数所在的代码是:

include_once('init.php');

function add_product($title, $category_id, $description, $price, $stock)
{
    $title =  mysql_real_escape_string($title);
    $category_id = (int)($category_id);
    $description = mysql_real_escape_string($description);
    $price = doubleval($price);
    $stock = (int)$stock;

    $mysql_query("INSERT INTO `products` SET
                `product_name` = '{$title}',
                `product_description` = '{$description}',
                `category_id` = {$category_id},
                `price` = {$price},
                `stock` = {$stock}");
}

我在数据库中显示产品的所有其他代码都可以正常工作,因此连接和配置都可以(我认为)。问题是当我尝试添加产品时,当我单击按钮提交网站时,它给了我一个服务器错误。

4

1 回答 1

-1

如果我错了,请纠正我,但这应该是

  if( isset($_POST['title'] && $_POST['icon_description'] && $_POST['category'] && $_POST['price'] && $_POST['stock']) ) {
                   //....
}

而不是在它们之间放置昏迷。

于 2012-10-05T15:38:50.247 回答