我正在尝试在数据库中插入数据。我收到这个错误:
获取数据时出错
这是我插入的表单代码......
<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>
这是我在同一个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); ?>
这是我的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;
答案 0 :(得分:0)
我认为问题发生在id
字段中,在您提供的声明中&#39;&#39;对于不能NULL
而且int
的ID。
因此,在您的表格中,在AUTO_INCREMENT
定义中添加id
:
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;
在您的查询中:
$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')";
并且应该有效。
答案 1 :(得分:0)
错误就在你的桌子上。 您指定了&#39; id&#39; as not null并将其作为主键。
但是你试图插入&#39;&#39;到id(主键)
解决方案:
对于&#39; id&#39;删除非空在表proj1
中
或使其自动增加