【发布时间】:2012-10-05 14:34:49
【问题描述】:
我在制作 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}");
}
我在数据库中显示产品的所有其他代码都在工作,所以连接和配置都可以(我认为)。问题是当我尝试添加产品时,当我单击按钮提交网站时,它给了我一个服务器错误。
【问题讨论】:
-
您查看过服务器错误日志吗?
-
当我看到您的评论时,我刚刚检查了它:“[xxx.yyy.com] [Fri Oct 05 14:56:58 2012] [error] [client xxx.xxx.xx] script not找到或无法统计:/home/domains/xxx.yyy.com/error404.html" ...
-
我认为那是你的标题行?也许尝试使用绝对路径而不是
products.php? -
找不到文件错误不是致命错误。这更像是一个警告。服务器输出什么样的错误? 500?
-
该页面与 products.php 位于同一根文件夹中,所以...