mysql查询没有插入数据库

时间:2012-05-01 18:02:19

标签: php mysql

我正在构建一个简单的表单来让用户请求耗材,但在提交时它会不断给我一个通用错误。我不知道我的错误在哪里......

     CREATE TABLE `teacherrequests` (
  `name` varchar(64) NOT NULL,
  `binder` varchar(3) NOT NULL,
  `binderpage` varchar(2) NOT NULL,
  `binderdiv` varchar(2) NOT NULL,
  `calcr` varchar(2) NOT NULL,
  `chalkcolor` varchar(2) NOT NULL,
  `chalkwhite` varchar(2) NOT NULL,
  `chalkeraser` varchar(2) NOT NULL,
  `colpencil` varchar(2) NOT NULL,
  `dryerasecleaner` varchar(2) NOT NULL,
  `dryeraser` varchar(2) NOT NULL,
  `dryerasemarkers` varchar(2) NOT NULL,
  `filefolders` varchar(2) NOT NULL,
  `gluesticks` varchar(2) NOT NULL,
  `hangingfile` varchar(2) NOT NULL,
  `highlightermulti` varchar(2) NOT NULL,
  `highlighterone` varchar(2) NOT NULL,
  `holepunch3` varchar(2) NOT NULL,
  `holepunch1` varchar(2) NOT NULL,
  `index35` varchar(2) NOT NULL,
  `index57` varchar(2) NOT NULL,
  `markers` varchar(2) NOT NULL,
  `papergraph` varchar(2) NOT NULL,
  `paperlegal` varchar(2) NOT NULL,
  `papersteno` varchar(2) NOT NULL,
  `paperwhitelined` varchar(2) NOT NULL,
  `paperclipsbindlg` varchar(2) NOT NULL,
  `paperclipsbindmd` varchar(2) NOT NULL,
  `paperclipsbindsm` varchar(2) NOT NULL,
  `paperclipslg` varchar(2) NOT NULL,
  `paperclipsmd` varchar(2) NOT NULL,
  `paperclipssm` varchar(2) NOT NULL,
  `pencil` varchar(2) NOT NULL,
  `pencilsharp` varchar(2) NOT NULL,
  `pensblack` varchar(2) NOT NULL,
  `pensblue` varchar(2) NOT NULL,
  `pensgreen` varchar(2) NOT NULL,
  `pensred` varchar(2) NOT NULL,
  `permanentmarker` varchar(2) NOT NULL,
  `postit` varchar(2) NOT NULL,
  `postitsmall` varchar(2) NOT NULL,
  `stapler` varchar(2) NOT NULL,
  `staples` varchar(2) NOT NULL,
  `tapedbl` varchar(2) NOT NULL,
  `tapemasking` varchar(2) NOT NULL,
  `tapescotch` varchar(2) NOT NULL,
  `tapedispenser` varchar(2) NOT NULL,
  `tacks` varchar(2) NOT NULL,
  `clock` varchar(2) NOT NULL,
  `whiteout` varchar(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

这是代码......

我已根据我的表格检查了这些内容,但它们都匹配。

$name = $_POST['name'];
$binder = $_POST['binder'];
$binderpage = $_POST['binderpage'];
$binderdiv = $_POST['binderdiv'];
$calc = $_POST['calc'];
$chalkcolor = $_POST['chalkcolor'];
$chalkwhite = $_POST['chalkwhite'];
$chalkeraser = $_POST['chalkeraser'];
$colpencil = $_POST['colpencil'];
$dryerasecleaner = $_POST['dryerasecleaner'];
$dryeraser = $_POST['dryeraser'];
$dryerasemarkers = $_POST['dryerasemarkers'];
$filefolders = $_POST['filefolders'];
$gluesticks = $_POST['gluesticks'];
$hangingfile = $_POST['hangingfile'];
$highlightermulti = $_POST['highlightermulti'];
$highlighterone = $_POST['highlighterone'];
$holepunch3 = $_POST['holepunch3'];
$holepunch1 = $_POST['holepunch1'];
$index35 = $_POST['index35'];
$index57 = $_POST['index57'];
$markers = $_POST['markers'];
$papergraph = $_POST['papergraph'];
$paperlegal = $_POST['paperlegal'];
$papersteno = $_POST['papersteno'];
$paperwhitelined = $_POST['paperwhitelined'];
$paperclipsbindlg = $_POST['paperclipsbindlg'];
$paperclipsbindmd = $_POST['paperclipsbindmd'];
$paperclipsbindsm = $_POST['paperclipsbindsm'];
$pencil = $_POST['pencil'];
$pencilsharp = $_POST['pencilsharp'];
$pensblack = $_POST['pensblack'];
$pensblue = $_POST['pensblue'];
$pensgreen = $_POST['pensgreen'];
$pensred = $_POST['pensred'];
$permanentmarker = $_POST['permanentmarker'];
$postit = $_POST['postit'];
$postitsmall = $_POST['postitsmall'];
$stapleremover = $_POST['stapleremover'];
$stapler = $_POST['stapler'];
$staples = $_POST['staples'];
$tapedbl = $_POST['tapedbl'];
$tapemasking = $_POST['tapemasking'];
$tapescotch = $_POST['tapescotch'];
$tapedispenser = $_POST['tapedispenser'];
$tacks = $_POST['tacks'];
$clock = $_POST['clock'];
$whiteout = $_POST['whiteout'];

我认为我的错误在这里,但我似乎无法找到它。

// Insert data into mysql 

$sql="INSERT INTO teacherrequests(name, binder, binderpage, binderdiv, calcr, 
chalkcolor, chalkwhite, chalkeraser, colpencil, dryerasecleaner, dryeraser,     
dryerasemarkers, filefolders, gluesticks, hangingfile, highlightermulti, highlighterone,     
holepunch3, holepunch1, index35, index57, markers, papergraph, paperlegal, papersteno,     
paperwhitelined, paperclipsbindlg, paperclipsbindmd, paperclipsbindsm, pencil,     
pencilsharp, pensblack, pensblue, pensgreen, pensred, permanentmarker, postit,     
postitsmall, stapleremover, stapler, staples, tapedbl, tapemasking, tapescotch,     
tapedispenser, tacks, clock, whiteout)

VALUES('$name', '$binder', '$binderpage',     
'$binderdiv', '$calc', '$chalkcolor', '$chalkwhite', '$chalkeraser', '$colpencil',     
'$dryerasecleaner', '$dryeraser', '$dryerasemarkers', '$filefolders', '$gluesticks',     
'$hangingfile', '$highlightermulti', '$highlighterone', '$holepunch3', '$holepunch1',     
'$index35', '$index57', '$markers', '$papergraph', '$paperlegal', '$papersteno',     
'$paperwhitelined', '$paperclipsbindlg', '$paperclipsbindmd', '$paperclipsbindsm',     
'$pencil', '$pencilsharp', '$pensblack', '$pensblue', '$pensgreen', '$pensred',     
'$permanentmarker', '$postit', '$postitsmall', '$stapleremover', '$stapler', '$staples',     
'$tapedbl', '$tapemasking', '$tapescotch', '$tapedispenser', '$tacks', '$clock',    
'$whiteout')";

$result=mysql_query($sql);

这是var_dump:

ERRORstring(811) "INSERT INTO teacherrequests(name, binder, binderpage, binderdiv, calcr, chalkcolor, chalkwhite, chalkeraser, colpencil, dryerasecleaner, dryeraser, dryerasemarkers, filefolders, gluesticks, hangingfile, highlightermulti, highlighterone, holepunch3, holepunch1, index35, index57, markers, papergraph, paperlegal, papersteno, paperwhitelined, paperclipsbindlg, paperclipsbindmd, paperclipsbindsm, pencil, pencilsharp, pensblack, pensblue, pensgreen, pensred, permanentmarker, postit, postitsmall, stapleremover, stapler, staples, tapedbl, tapemasking, tapescotch, tapedispenser, tacks, clock, whiteout)VALUES('Joe Spano', '3', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '4', '', '', '', '', '', '2', '', '', '', '', '', '', '', '', '', '5', '', '', '', '', '', '', '', '')"

2 个答案:

答案 0 :(得分:14)

您应该做的第一件事就是采用这种数据库设计并将其丢入垃圾箱。然后用这个设计替换它:

CREATE TABLE teachingmaterials (
    id SMALLINT NOT NULL AUTO_INCREMENT,
    name VARCHAR(64) NOT NULL,
    PRIMARY KEY (id)
) ENGINE=INNODB CHARACTER SET utf8;

INSERT INTO
    teachingmaterials
    (name)
VALUES
    ('binder'),
    ('binder-page'),
    ('binder-div'),
    -- put the rest of them in here --
    ('whiteout');

CREATE TABLE teacherrequests (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(64) NOT NULL,
    PRIMARY KEY (id)
) ENGINE=INNODB CHARACTER SET utf8;

CREATE TABLE teacherrequests_teachingmaterials (
    teacherrequests_id INT NOT NULL,
    teachingmaterials_id SMALLINT NOT NULL,
    amount SMALLINT,
    note VARCHAR(64),
    PRIMARY KEY (teacherrequests_id, teachingmaterials_id)
    FOREIGN KEY (teacherrequests_id) REFERENCES teacherrequests (id) ON DELETE CASCADE,
    FOREIGN KEY (teachingmaterials_id) REFERENCES teachingmaterials (id)
) ENGINE=INNODB CHARACTER SET utf8;

您还需要停止使用旧的mysql_*功能,转而使用PDO。

答案 1 :(得分:7)

看看

 `calcr` varchar(2) NOT NULL,

相比,

创建表代码

$calc = $_POST['calc'];
在你的PHP代码中

。我猜这只是一个简单的拼写错误,最后有额外的“r”。

我还发现这些代码行在另一个文件中没有附带的行:

  `paperclipslg` varchar(2) NOT NULL,
  `paperclipsmd` varchar(2) NOT NULL,
  `paperclipssm` varchar(2) NOT NULL,

  $stapleremover = $_POST['stapleremover'];

希望有所帮助!

[编辑] 我没有看到您的插入内容与您的值之间存在任何差异。所以现在我更多地看待这个,我认为我没有帮助解决问题。我只找到了一些你可能遗漏的数据。