插入记录失败! php mysqli

时间:2009-11-06 16:27:10

标签: php mysqli

我有一个mysql数据库...

CREATE TABLE `applications` (
`id` int(11) NOT NULL auto_increment,
`jobref` int(11) NOT NULL,
`userid` int(11) NOT NULL,
`q1` text NOT NULL,
`q2` text NOT NULL,
`q3` text NOT NULL,
`sub_q1` text,
`sub_q2` text,
`sub_q3` text,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`printed` int(11) NOT NULL default '0',
PRIMARY KEY  (`id`),
KEY `jobref` (`jobref`),
KEY `applications_ibfk_2` (`userid`),
CONSTRAINT `applications_ibfk_1` FOREIGN KEY (`jobref`) REFERENCES `jobs` (`id`) ON     DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `applications_ibfk_2` FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON  DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='InnoDB free: 9216 kB; (`jobref`) REFER `iwcjobs/jobs`(`id`) '

和这个php文件,使用mysqli进行数据操作。

<?php

require_once '../includes/constants.php';

if(isset($_POST['submit'])) {
$q1 = $_POST['question1'];
$q1a = $_POST['ifNoQuestion1'];
$q2 = $_POST['question2'];
$q2a = $_POST['ifNoQuestion2'];
$q3 = $_POST['question3'];
$q3a = $_POST['ifNoQuestion3'];
$JobRef = $_POST['jobref'];
$UserRef = $_POST['id'];

$mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database');

if($stmt = $mysql->prepare('INSERT INTO `applications` VALUES (NULL,?,?,?,?,?,?,?,?,NULL,NULL)')) {
  $stmt->bind_param('iissssss',$JobRef,$UserRef,$q1,$q2,$q3,$q1a,$q2a,$q3a);
  $stmt->execute();
  $stmt->close();

  header('location: ../myApps.php?apr=y');
  //echo ("<h2>success</h2> - $q1 . $q2 . $q3 . $q1a . $q2a . $q3a . $JobRef . $UserRef");

} else {
  echo 'error: ' . $mysql->error;
}

} else {

echo 'errorStage2: ' . $mysql->error;

}

?>

脚本从前一个表单中获取正确的值,但不将它们插入到数据库中。人们有什么想法? Thx提前, 亚伦。

1 个答案:

答案 0 :(得分:1)

当前编写插入查询的方式,您尝试将id设置为NULL,这是一个NOT NULL字段。

最好将插入结构如下:

INSERT INTO 'applications' (jobref,userid,etc..) VALUES (?,?etc..)

这样,你不要试图改变id,只要让auto_increment做就好了。