为什么我在这里得到“外键约束失败......”?

时间:2015-08-21 19:01:09

标签: php mysql sql database-design foreign-keys

我有一个功能

function proj_update ( )
{


    $id = $_POST['id'];
    $compname = $_POST['compname'];
    $projname = $_POST['projname'];
    $imageurl = $_POST['imageurl'];
    $sumsmall = $_POST['sumsmall'];
    $sumfull = $_POST['sumfull'];
    $results = $_POST['results'];
    $caseid = (!isset($_POST['caseid']) || strcmp($_POST['caseid'],'none')) ? $_POST['caseid'] : NULL; // weirdness required to get the value of our <select name="caseid"> element converted to something we can insert to database
    $hide = array_key_exists('hide',$_POST) !== false ?  1 : 0; // weirdness required to get the value of <input type="checkbox" name="hide"> converted to something we can insert to database

    $thisAction = $_POST['thisAction']; 

    global $wpdb;

    $message = "";

    switch ($thisAction)
    {
        case 'add':
        {
            $addQuery = $wpdb->prepare("INSERT INTO projs (compname,projname,imageurl,sumsmall,sumfull,results,caseid,hide) VALUES (%s,%s,%s,%s,%s,%s,%d,%d)",
                                        array($compname, $projname,$imageurl,$sumsmall,$sumfull,$results,$caseid,$hide));       
            $message .= $wpdb->query($addQuery) 
                        ? 'Successfully added project to the database.'
                        : 'Error occurred when trying to add project to database: ' . $wpdb->last_error;
            break;
        }
        case 'delete':
        {
            $deleteQuery = $wpdb->prepare("DELETE FROM projs WHERE id=%d", $id);
            $message .= $wpdb->query($deleteQuery)
                      ? 'Successfully deleted project from the database.'
                      : 'Error occurred when trying to delete project from database: ' . $wpdb->last_error;
            break;
        }
        case 'update':
        {
            $updateQuery = $wpdb->prepare("UPDATE projs SET compname=%s,projname=%s,imageurl=%s,sumsmall=%s,sumfull=%s,results=%s,caseid=%d,hide=%d WHERE id=%d",
                                          array($compname, $projname, $imageurl, $sumsmall, $sumfull, $results, $hide,$caseid,$id));
            $message .= $wpdb->query($updateQuery) !== false
                        ? 'Successfully updated project.'
                        : 'Error occurred when trying to update project in database: ' . $wpdb->last_error;
            break;
        }
        default:
        {
            $message .= 'Didn\'t recognize action.'; 
            break;
        }
    }
    die($message);   

}

处理AJAX调用,而我遇到的问题是使用我数据库的caseid列的语句,特别是

INSERT INTO projs (compname,projname,imageurl,sumsmall,sumfull,results,caseid,hide) VALUES (%s,%s,%s,%s,%s,%s,%d,%d)

UPDATE projs SET compname=%s,projname=%s,imageurl=%s,sumsmall=%s,sumfull=%s,results=%s,caseid=%d,hide=%d WHERE id=%d

当我尝试插入NULL值时,它们都会一直失败。

我得到的错误是

  

外键约束失败('my_database_name','projs',CONSTRAINT   'projs_ibfk_1'FOREIGN KEY('caseid')REFERENCES'case''('id')ON   DELETE SET NULL

由于表projs是使用查询

创建的
$wpdb->query("CREATE TABLE projs (id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, compname VARCHAR (200), projname VARCHAR (200), imageurl VARCHAR(300), sumsmall VARCHAR (250), sumfull VARCHAR (5000), results VARCHAR (5000), postdate DATETIME DEFAULT CURRENT_TIMESTAMP, caseid MEDIUM INT, FOREIGN KEY (caseid) REFERENCES cases(id) ON DELETE SET NULL) $charset_collate");

对于NOT NULL的定义没有caseid,我不确定为什么它不允许我将其设置为NULL

0 个答案:

没有答案