-1
if(empty($Errormessage))
            {
                    if(move_uploaded_file($File_Tmp,"product_images/".$File_Name))
                    {
                        //To Rename the uploaded file
                        $Random=rand()*1200;
                        $File_New_Name=$Random.".".$File_Extension;
                        rename("product_images/".$File_Name,"product_images/".$File_New_Name);  
                        $Date_Added=date("d-m-Y");
                        $Query="INSERT INTO products (pro_id,product_name,price,details,product_picture,date_added,last_edited,cat_id) VALUES ('','$Product_Name','$Product_Price','$Product_Details','$File_New_Name','$Date_Added','','$Category')";
                        $Result=mysql_query($Query);
                    if($Result)
                        {
                            header ("location: addsuccess.php");
                        }

                    }
            }

//类别

<td><select name="category" size="1" id="select">
      <option value="Null">Select Category</option>
      <?php
      $Select=mysql_query("SELECT * FROM category");
      while ($Row=mysql_fetch_array($Select))
      { 
            $Cat_Id=$Row['cat_id'];
            $Category_Name=$Row['category_name'];
      ?>
        <option value="$Cat_Id"><?php echo $Category_Name; ?></option>
      <?php } ?>
    </select></td>

//验证

if($Category=="Null")
        {
            $Errormessage[]="Category should be selected";
        }

cat_id 是我的产品表中的 FK 列...而且 cat_id 是类别表中的主键...因为我将此 cat_id 设为 FK 实际上我复制粘贴了 alter table 添加列 fk 等的语法等...它不是让我保存我的记录......我通过传递虚拟值直接在我的管理员上尝试了它......它正在工作。

4

2 回答 2

0

尝试:

$Query="INSERT INTO products (pro_id, product_name,price,details,product_picture,date_added,last_edited,cat_id) VALUES (NULL,'$Product_Name','$Product_Price','$Product_Details','$File_New_Name','$Date_Added','',$Category)";

NULL 而不是 '' 可能会解决您的问题。$Category 现在也没有撇号,因为 db-column cat_id 是一个数字而不是一个字符串。(数字不应使用撇号处理)。

$Category=mysql_real_escape_string($_POST['category']); 

(您试图转义一个字符串,但 $_POST['category'] 是一个数字),所以这不是正确的做法)。

It should be something like:

if (isset($_POST['category'])) {
   $Category = $_POST['category'];
}
else {
   $Category = 0;
}

Also change:

  ?>
    <option value="$Cat_Id"><?php echo $Category_Name; ?></option>
  <?php } ?>

TO

  ?>
    <option value="<?php echo $Cat_Id;?>"><?php echo $Category_Name; ?></option>
  <?php } ?>

It's also strongly recommended to use PDO or Mysqli. It will save a lot of time and problems for the future. mysql-functions like mysql_query is deprecated and is going to be removed in future releases of php.

于 2013-03-30T22:26:23.700 回答
0

So that's the error. There is mistake on your cat_id reference. Check the db constraints. $Category is INSERTED mistakenly. E.g. If cat_id is connected with an other table, you cannot insert it with $Category.

于 2013-03-30T22:33:37.267 回答