0

ドロップダウンから選択した値に応じて、データをテーブルに挿入したいと思います。実際、私には2つのテーブルがCategoriesありProductsます。両方とも。と相互リンクされていCategoryIDます。
これでフォームができました。製品を追加しようとしています。カテゴリのドロップダウンがあります。ドロップダウンからカテゴリを選択し、そのカテゴリに関連製品を追加します。

フォームデータを取得した後、次のCategoryIDようにテーブルをフェッチしました。

 include('includes/conn.php');
 if (isset($_POST['submit']))
 { 
     $CategoryName = $_POST['cat'];
     echo $CategoryName; 
     $ProductName = mysql_real_escape_string(htmlspecialchars($_POST['ProductName']));
     $ProductCode = $_POST['ProductCode'];
     $Specification = $_POST['Specification'];
     $Description = $_POST['Description'];
     $CostPrice = $_POST['CostPrice'];
     $DisplayPrice = $_POST['DisplayPrice'];
     $ProductID = $_POST['ProductID'];
     $Productimage = $_POST['ProductImage'];
     $sql = "select * Categories";
     $result = mysql_query ($sql);
     $row = mysql_fetch_array($result);
     $Category_ID = $row['CategoryID'];

この後、私はやり方がわかりません。その条件を除いて、私のコードはレコードを正常に挿入します。このようなcategoryidを選択せず​​に私の完全なコード

<?php 
}

include('includes/conn.php');
    if (isset($_POST['submit']))
    { 
        $ProductName = mysql_real_escape_string(htmlspecialchars($_POST['ProductName']));
        $ProductCode = $_POST['ProductCode'];
        $Specification = $_POST['Specification'];
        $Description = $_POST['Description'];
        $CostPrice = $_POST['CostPrice'];
        $DisplayPrice = $_POST['DisplayPrice'];
        $ProductID = $_POST['ProductID'];
        $Productimage = $_POST['ProductImage'];
        if ($ProductName == '' || $ProductCode == ''|| $Specification == '' || $Description == '' || $CostPrice == '' || $DisplayPrice =='')
        {
            echo  "Please fill in all required fields";
            renderForm($ProductID, $ProductName, $ProductCode, $Description, $Specification, $CostPrice, $DisplayPrice,  $error);
        }
        else
        {
            $sql = "INSERT into Products SET ProductName='$ProductName', ProductCode='$ProductCode', Specification ='$Specification', Description = '$Description', CostPrice = $CostPrice, DisplayPrice = $DisplayPrice, ProductImage = '$ProductImage'";
            mysql_query($sql) or die(mysql_error());
            echo " Successfully Added "; 
            //header("Location: view.php"); 
        }
    }
    else
    {
        renderForm('','','','','','','','','');
    }
   ?> 

それを行う方法を提案してください?

4

1 に答える 1

2
$sql = "INSERT INTO Products (ProductName, ProductCode, ...) VALUES ('".$ProductName."', '". $ProductCode ."', ...";

これは、挿入クエリを使用する方法です。

http://www.tizag.com/mysqlTutorial/mysqlinsert.php

于 2012-05-25T07:24:11.020 に答える