ドロップダウンから選択した値に応じて、データをテーブルに挿入したいと思います。実際、私には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('','','','','','','','','');
}
?>
それを行う方法を提案してください?