简体   繁体   English

无法插入外键值

[英]Cannot insert foreign key value

I've made two tables to store user information, and the photo information they uploaded. 我制作了两个表来存储用户信息以及他们上传的照片信息。 Everything goes well, except when I try to insert userID to photos table, mysql just don't take it. 一切顺利,除非当我尝试将userID插入到photos表中时,mysql才不接受它。 The problem might comes from the foreign key things, but I don't know how to fix it. 问题可能出在外键,但我不知道如何解决。

CREATE TABLE IF NOT EXISTS users (
  userID smallint(7) NOT NULL auto_increment,
  email varchar(60) collate utf8_unicode_ci NOT NULL default '',
  PRIMARY KEY  (userID),
  UNIQUE KEY email (email)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE IF NOT EXISTS photos (
 photoID MEDIUMINT(7) NOT NULL AUTO_INCREMENT,
 userID SMALLINT(7),
 title       VARCHAR(150),
 brief    TEXT,
 PRIMARY KEY(photoID),
 INDEX (userID),
 FOREIGN KEY (userID)
 REFERENCES users(userID)
 ) ENGINE=INNODB; 

And the sql command I used to insert information to photos table 我用来将信息插入到照片表的sql命令

$sql = "INSERT INTO photos (userID ,title, brief)".
       "VALUES ('$uid', '$title','$bodytext')";

Even if the $uid is replaced by a static number like '2', which exists in user table(userID), it's still cannot be inserted. 即使$uid替换为用户表(userID)中存在的静态数字(如“ 2”),也仍然无法插入。

I took some advices to add mysql_error function, however, it doesn't show anything... 我建议添加mysql_error函数,但是它什么也没显示。

$sql = "INSERT INTO photos (userID ,title, brief)".
       "VALUES ('2', '$title','$bodytext')";
$res = mysql_query($sql);

if ($res === FALSE) {
  die(mysql_error());
}

return mysql_query($sql);

Have you checked the exact error message? 您检查了确切的错误消息吗? the MySQL functions return 'FALSE' if the query fails: 如果查询失败,MySQL函数将返回“ FALSE”:

$sql = "INSERT ...";
$res = mysql_query($sql);
if ($res === FALSE) {
    die(mysql_error());
}

This will output the exact reason the query is failing. 这将输出查询失败的确切原因。 Until you do this, we can at most guess at what the problem is. 在执行此操作之前,我们最多只能猜测问题出在哪里。

Most likely the error is due to the extra comma you have in your values field: 该错误很可能是由于您在“值”字段中有多余的逗号引起的:

$sql = "INSERT INTO photos (`userID` ,title, brief, )".
                                                  ^---here

that causes a syntax error. 导致语法错误。

Two simple debugging tips: 两个简单的调试技巧:

<1> Inspect the real SQL code you are sending to the server. <1>检查要发送到服务器的真实SQL代码。 Add this line to your PHP code: 将此行添加到您的PHP代码中:

echo htmlspecialchars($sql);

... find the query in the browser and paste it in your MySQL client (HeidiSQL, PHPMyAdmin or whatever). ...在浏览器中找到查询,并将其粘贴到您的MySQL客户端(HeidiSQL,PHPMyAdmin或其他)中。 Pay close attention to the possible error messages. 请密切注意可能的错误消息。

<2> Check error conditions in all functions: <2>检查所有功能中的错误情况:

$con = mysql_connect(...);
if( !$con ){
   die( htmlspecialchars(mysql_error());
}
...
$res = mysql_query(...);
if( !$res ){
   die( htmlspecialchars(mysql_error());
}

You can't insert value for foreign key like all others. 您不能像其他所有插入键一样插入外键的值。 Take example from my code: 以我的代码为例:

  mysql_query("INSERT INTO 'bikemodel' VALUES('1', 'Cougar 125', '2009', 'Scooter CVT 1sp 150cc', 'Scooter', 'Road Bike', '150', foreign key=1)");

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

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