简体   繁体   English

无法在MySQL中使用外键插入记录?

[英]Can't insert record with foreign key in MySQL?

I've got a user table, and a picture table. 我有一个用户表和一个图片表。

The picture table has an uploader id column which is a foreign key. 图片表具有一个上载者ID列,该列是一个外键。 It refers to the user id column in the user table. 它引用用户表中的用户ID列。

For some reason, when I try to insert a new record into the picture table, it's not working. 由于某种原因,当我尝试将新记录插入到图片表中时,它不起作用。 No error messages pop up. 没有错误消息弹出。 It just doesn't insert the new record. 它只是不插入新记录。

INSERT INTO picture (pic_url, pic_uploader) VALUES($picurl, $uploader);

$picurl is an image src path from a file upload. $ picurl是来自文件上传的图像src路径。 The uploaded files are in the right directory, and the exact same code works perfectly for an earlier record without a foreign key. 上载的文件位于正确的目录中,并且完全相同的代码可以完美地用于没有外键的早期记录。

$uploader contains the foreign key value -- from a session variable that contains the user id of the user account -- but it's not inserting it into the table. $ uploader包含外键值(来自包含用户帐户的用户ID的会话变量),但未将其插入表中。

Here's the SQL for the tables, in case that helps... 这是表的SQL,以防万一。

Picture table 图片表

CREATE TABLE IF NOT EXISTS picture (
  pic_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  pic_url varchar(200) NOT NULL,
  pic_uploader bigint(20) unsigned NOT NULL,
  PRIMARY KEY (pic_id),
  KEY pic_uploader (pic_uploader)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Picture constraints 图片约束

ALTER TABLE picture
  ADD CONSTRAINT pictures_ibfk_1 FOREIGN KEY (pic_uploader) REFERENCES user (user_id);

User table 用户表

CREATE TABLE IF NOT EXISTS user (
  user_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  user_name varchar(80) NOT NULL,
  user_img varchar(200) NOT NULL DEFAULT 'img/defaultpic.png',
  user_email varchar(100) NOT NULL,
  user_pword char(60) NOT NULL,
  user_stat enum('0','1','A') NOT NULL,
  PRIMARY KEY (user_id),
  UNIQUE KEY user_email (user_email)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

There can be a reason that $uploader value does not exists in your user table as user_id. $ uploader值作为user_id在用户表中不存在可能是有原因的。

First try to insert in simple way directly in db by any tool like sqlyog putty, mysqladmin etc. 首先尝试通过任何工具(例如sqlyog putty,mysqladmin等)以简单的方式直接在db中插入。

INSERT INTO picture (pic_url, pic_uploader) VALUES('pic1', 'user1');

Make sure user1 should exist in user table. 确保user1应该存在于用户表中。 If it works fine then problem in your application other wise mysql will return an error by this we will be able to check the exact problem either it is due to foreign key or else. 如果工作正常,那么您的应用程序中的问题会导致其他错误,否则mysql将返回错误,我们将能够检查由于外键导致的确切问题。 So do it and share results. 这样做并分享结果。

IN your picture table you are using columns NOT NULL. 在图片表中,您使用的列不是NULL。

But you try to insert only two values in the insert command that's why data is not inserted. 但是您尝试在insert命令中仅插入两个值,这就是为什么不插入数据的原因。

Either insert data for all columns or make columns as NULLABLE. 插入所有列的数据或将列设置为NULLABLE。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM