English 中文(简体)
根据 ID 将值插入数据库
原标题:inserting a value into database depending on id
  • 时间:2012-05-25 07:17:34
  •  标签:
  • php
  • mysql

I want to insert my data into a table depending on the value I select from the drop down. Actually I have two tables, Categories and Products. Both are interlinked with CategoryID.
Now I have a form through this I am trying to add a product. I have a dropdown for categories. I will select a category from the dropdown and then I will add related products to that category.

获取窗体数据后, 我从 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 ];

after this I am not getting how to do. Except that condition my code inserts the record successfully. my complete code withouth selecting the categoryid like this

<?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(  ,  ,  ,  ,  ,  ,  ,  ,  );
    }
   ?> 

请建议如何做?

最佳回答
$sql = "INSERT INTO Products (ProductName, ProductCode, ...) VALUES ( ".$ProductName." ,  ". $ProductCode ." , ...";

这就是您如何使用插入查询的方式 。

http://www.tizag.com/mysqlTumental/mysqlIndeption.php" rel=“no follow'>http://www.tizag.com/mysqlTumental/mysqlIndeption.php

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签