尝试使用php在数据库中插入数据时出错

时间:2021-12-02 14:37:08

I am trying to insert data in a database. I get this error:

我试图在数据库中插入数据。我收到此错误:

error getting data

获取数据时出错

This is my form code for inserting...

这是我插入的表单代码...

<body>

    <form action="#" method="post">
    <fieldset>
    <legend><b>Enter the data:</b></legend>
    <p>Pick a Date: <input type="date" id="datepicker" name="date" /></p>
        <p>SANC AMT
        <select name="f2" size="1">
            <option value="">Select Option</option>
            <option value="NO SANCTION">NO SANCTION</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select></p>

        <p>FORM SALE <select name="f3" size="1">
            <option value="">Select Option</option>
            <option value="NO SALE">NO SALE</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select> </p>
        <p>FORM CHAN <input type="text" name="f4"></p>
        <p>FORM SUB 1st <input type="text" name="f5"></p>
        <p>FORM REF <input type="text" name="f6"></p>
        <p>FORM REJ <input type="text" name="f7"></p>
        <p>F SUB 2nd <input type="text" name="f8"></p>
        <p>F SUB 3rd <input type="text" name="f9"></p>
        <p>FUEL <input type="text" name="f10"></p>

        <p><input type="submit" name="submit" value="submit"></p>

    </fieldset>
    </form>


</body>

This is my php code for insertion inside same php file

这是我在同一个php文件中插入的php代码

<?php
        $conn = mysql_connect("localhost","root","");
        $db = mysql_select_db("proj",$conn);

    if(isset($_POST['submit'])) 
    {
    $date = $_POST['date'];
    $f2 = $_POST['f2'];
    $f3 = $_POST['f3'];
    $f4 = $_POST['f4'];
    $f5 = $_POST['f5'];
    $f6 = $_POST['f6'];
    $f7 = $_POST['f7'];
    $f8 = $_POST['f8'];
    $f9 = $_POST['f9'];
    $f10 = $_POST['f10'];

    $sql     = "INSERT into proj1 values('','$date','$f2','$f3','$f4','$f5', '$f6','$f7','$f8','$f9','$f10')";

    $query  = mysql_query($sql) or die('error getting data');

    if($query)
    {
        echo "<table align='center'>";
        echo '<h1 style="text-align:center;">Inserted</h1><br />';
        echo '<h3 style="text-align:center;"><i>You have inserted ....</i></h3>';
        echo '<tr><th>Date:</th><td> ' .$date. '</td><tr>';
        echo '<tr><th>Sanc amt </th><td> ' .$f2. '</td><tr>';
        echo '<tr><th>Form SALE</th><td>  ' .$f3. '</td><tr>';
        echo '<tr><th>FORM CHan </th><td> ' .$f4. '</td><tr>';
        echo '<tr><th>FORM Sub 1st </th><td> ' .$f5. '</td><tr>';
        echo '<tr><th>FORM ref </th><td> ' .$f6. '</td><tr>';
        echo '<tr><th>FORM rej</th><td> ' .$f7. '</td><tr>';
        echo '<tr><th>F sub 2nd</th><td>  ' .$f8. '</td><tr>';
        echo '<tr><th>F sub 3rd</th><td> ' .$f9. '</td><tr>';
        echo '<tr><th>FUEL</th><td> ' .$f10. '</td><tr>';
        echo "</table>";
    }
    }
 mysql_close($conn); ?>

This is my SQL statement

这是我的SQL语句

CREATE TABLE IF NOT EXISTS `proj1` (
  `id` int(11) NOT NULL,
  `DATE` date NOT NULL,
  `sanc_amt` varchar(11) DEFAULT NULL,
  `form_sale` varchar(11) DEFAULT NULL,
  `form_chan` varchar(11) DEFAULT NULL,
  `form_sub` varchar(11) DEFAULT NULL,
  `form_ref` varchar(11) DEFAULT NULL,
  `form_rej` varchar(11) DEFAULT NULL,
  `f_sub2` varchar(11) DEFAULT NULL,
  `f_sub3` varchar(11) DEFAULT NULL,
  `fuel` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2 个解决方案

#1


0  

I think the problem is in id field, in your statement you provide '' for id that can't be NULL and it's a int.

我认为问题出在id字段中,在你的语句中,你提供的''对于不能为NULL的id,它是一个int。

So in your table add AUTO_INCREMENT in id definition:

所以在你的表中在id定义中添加AUTO_INCREMENT:

CREATE TABLE IF NOT EXISTS `proj1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `DATE` date NOT NULL,
  `sanc_amt` varchar(11) DEFAULT NULL,
  `form_sale` varchar(11) DEFAULT NULL,
  `form_chan` varchar(11) DEFAULT NULL,
  `form_sub` varchar(11) DEFAULT NULL,
  `form_ref` varchar(11) DEFAULT NULL,
  `form_rej` varchar(11) DEFAULT NULL,
  `f_sub2` varchar(11) DEFAULT NULL,
  `f_sub3` varchar(11) DEFAULT NULL,
  `fuel` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And in your query:

在您的查询中:

$sql="INSERT into proj1 (DATE, sanc_amt, form_sale, form_chan, form_sub, form_ref, form_rej, f_sub2, f_sub3, fuel) values('$date','$f2','$f3','$f4','$f5', '$f6','$f7','$f8','$f9','$f10')";

And should works.

并且应该有效。

#2


0  

The error is you in your table. You specified 'id' as not null and make it primary key too.

错误就在你的桌子上。您将'id'指定为非null并将其设为主键。

But you are trying to insert '' to id(Primary key)

但是你试图插入''到id(主键)

Solution : Remove not null for 'id' in the table proj1 or make it auto increment

解决方案:在表proj1中为'id'删除not null或使其自动递增

#1


0  

I think the problem is in id field, in your statement you provide '' for id that can't be NULL and it's a int.

我认为问题出在id字段中,在你的语句中,你提供的''对于不能为NULL的id,它是一个int。

So in your table add AUTO_INCREMENT in id definition:

所以在你的表中在id定义中添加AUTO_INCREMENT:

CREATE TABLE IF NOT EXISTS `proj1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `DATE` date NOT NULL,
  `sanc_amt` varchar(11) DEFAULT NULL,
  `form_sale` varchar(11) DEFAULT NULL,
  `form_chan` varchar(11) DEFAULT NULL,
  `form_sub` varchar(11) DEFAULT NULL,
  `form_ref` varchar(11) DEFAULT NULL,
  `form_rej` varchar(11) DEFAULT NULL,
  `f_sub2` varchar(11) DEFAULT NULL,
  `f_sub3` varchar(11) DEFAULT NULL,
  `fuel` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And in your query:

在您的查询中:

$sql="INSERT into proj1 (DATE, sanc_amt, form_sale, form_chan, form_sub, form_ref, form_rej, f_sub2, f_sub3, fuel) values('$date','$f2','$f3','$f4','$f5', '$f6','$f7','$f8','$f9','$f10')";

And should works.

并且应该有效。

#2


0  

The error is you in your table. You specified 'id' as not null and make it primary key too.

错误就在你的桌子上。您将'id'指定为非null并将其设为主键。

But you are trying to insert '' to id(Primary key)

但是你试图插入''到id(主键)

Solution : Remove not null for 'id' in the table proj1 or make it auto increment

解决方案:在表proj1中为'id'删除not null或使其自动递增