我一直在处理一个表单来向数据库添加数据。我有验证,让用户知道字段是空的还是无效的。一旦验证完毕,我想将其提交到数据库。我当前的代码验证数据但无论如何提交。这可能是我的逻辑中的一个缺陷,如果是其他陈述,但我似乎无法让它发挥作用。
以下是代码的一部分,包括一些验证和INSERT语句。
// Quantity
if (empty($_POST["qtyAdd"]))
{
$qtyErr = "Please enter a product quantity";
}
else
if (!is_numeric($year))
{
$qtyErr = "Data entered was not numeric";
}
else
if (strlen($number) != 3)
{
$qtyErr = "The number entered was not 3 digits long";
}
else
{
$qty = test_input($_POST["yearAdd"]);
}
// Featured
if (empty($_POST["featuredAdd"]))
{
$featuredErr = "Feature Item?";
}
else
{
$featured = test_input($_POST["featuredAdd"]);
}
//Add to database
{
$sql = "INSERT INTO tbl_products (product_name, product_description, product_price, product_year, product_show, product_type, product_signed, product_quantity, product_featured, product_img) VALUES ('$name', '$desc', '$price', '$year', '$show', '$cat', '$signed', '$qty', '$featured', $img)";
mysql_query($sql);
}
}
答案 0 :(得分:0)
你没有打算检查是否有错误。这就是为什么你的查询总是被执行的原因:
if (!isset($qtyErr) && !isset($featuredErr)) {
$sql = "INSERT INTO tbl_products (product_name, product_description, product_price, product_year, product_show, product_type, product_signed, product_quantity, product_featured, product_img) VALUES ('$name', '$desc', '$price', '$year', '$show', '$cat', '$signed', '$qty', '$featured', $img)";
mysql_query($sql);
}