使用PHP插入空值的数据

时间:2014-03-20 17:36:42

标签: php

这里order_ID,比尔可能是null,客户不需要填写表格,,,这里是php代码,,,,,它不工作.....我不知道如何插入元组与通过使用php.i找不到错误,将值赋值到表中。

create table Orders(
    Order_ID number(10) primary key,
    Cust_id number(5),
    Order_date date,
    Bill number(5,2),
    CONSTRAINT fk_cust  FOREIGN KEY (Cust_ID) REFERENCES Customer(Cust_ID)
    );


CREATE SEQUENCE ord_seq;

CREATE OR REPLACE TRIGGER table_res 
BEFORE INSERT ON Orders 
FOR EACH ROW

BEGIN
  SELECT ord_seq.NEXTVAL INTO   :new.Order_ID FROM   dual;
END;
/
   <?php
        $conn=oci_connect("system","123","localhost/orcl");
        ob_start();
        $current_file=$_SERVER['SCRIPT_NAME'];
        $massage= "";
        if(isset($_POST['Cust_id'])&&
        isset($_POST['Order_date']))           
        {

            $Cust_id= $_POST['Cust_id'];
            $Order_date = $_POST['Order_date'];
            if(!empty($Cust_id)&&!empty($Order_date))
            {

                    $sql = "insert into Orders values('".NULL."','".$Cust_id."','".$Order_date."','".NULL."')";
                    $stid = oci_parse($conn,$sql);
                    $r = @oci_execute($stid);
                    if($r)
                    {
                        echo ' data is inserted...<br>';
                    }
                    else
                    {
                        echo 'data was not inserted...<br>';
                    }

            }
            else
            {
                $massage = "please fill up all the form correctly<br>";
            }
        }

    ?>
    <html>
    <head>
    <title>Create FoodItem Table</title>
    <style>
    body
    {
    background:orange;
    }
    </style>
    <head>
    <body>
    You dont need to fill Order_ID and Bill<br><br>
    <?php echo $massage;?>
    <hr color="green">
    <form action="<?php echo $current_file;?>" method="POST">

        Cust_id:<br> <input type="text" name ="Cust_id" ><br><br>
        Order_date:<br> <input type="text" name="Order_date" ><br><br>

        <input type ="submit" value="Submit Order"><br><br>
        //<a href="EmployeeTableshow.php">Home</a>

    </form>
    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

插入像这样的空值

$sql="insert into Orders values(NULL,'".$Cust_id."','".$Order_date."',NULL)";

答案 1 :(得分:0)

在主键上插入NULL值可能不是您想要的。为您的密钥插入唯一值:

 $sql = "insert into Orders values('". $someUniqueValueThatYouCreated . "','".$Cust_id."','".$Order_date."','".NULL."')";

- 或 -

使用auto_increment改变你的表结构:

 Order_ID int(10) primary key auto_increment,

然后修改您的插入内容:

 $sql = "insert into Orders (Cust_id, Order_date, Bill) values('".$Cust_id."','".$Order_date."','".NULL."')";