在php中新创建的表中插入返回'表table_name不存在'

时间:2013-10-14 00:17:28

标签: php mysql sql

完全失败。基本上,脚本在mysql中创建表并用假期填充它们。

$link = mysqli_connect("127.0.0.1", "root", "root", "wolla");
if ( !$link ) {
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

$createTablesQuery1 = "SET foreign_key_checks = 0;";
$createTablesQuery2 = "DROP TABLE IF EXISTS `calendarCountries`;";
$createTablesQuery3 = "CREATE TABLE `calendarCountries` ( `id` int(5) NOT NULL AUTO_INCREMENT, `country` varchar(50) NOT NULL, PRIMARY KEY (`id`)) ENGINE = InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET = latin1;";
$createTablesQuery4 = "insert into calendarCountries (id,country) values (1,'US');";
$createTablesQuery5 = "DROP TABLE IF EXISTS `calendarWorldHolidays`;";
$createTablesQuery6 = "CREATE TABLE `calendarWorldHolidays` (`holidayName` varchar(150) NOT NULL,`holidayDate` date NOT NULL,`countryCode` int(5) DEFAULT NULL,KEY `countryCode` (`countryCode`),CONSTRAINT `calendarWorldHolidays_ibfk_1` FOREIGN KEY (`countryCode`) REFERENCES `calendarCountries` (`id`) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET = latin1;";
$createTablesQuery7 = "SET foreign_key_checks = 1;";

if ( !mysqli_multi_query( $link, $createTablesQuery1.$createTablesQuery2.$createTablesQuery3.$createTablesQuery4.$createTablesQuery5.$createTablesQuery6.$createTablesQuery7 ) )
    die( mysqli_error( $link ).'<br>');
    mysqli_close( $link );

    //INSERT HOLIDAYS INTO calendarWorldHolidays
    $link = mysqli_connect("127.0.0.1", "root", "root", "wolla");
    mysqli_query( $link, "SET foreign_key_checks = 0;");
    $count = 0;
    $holidayCount = count( $holidayNames );
    while ( $count < $holidayCount ) {
        $insertHolidayQuery = 'Insert into calendarWorldHolidays (holidayName, holidayDate, countryCode) values ("'.$holidayNames[$count].'", '.$holidayDates[$count].'", 1)';
        if ( !mysqli_query( $link, $insertHolidayQuery ) ) {
        die(mysqli_error($link).'<br>');
        } else {
        $count++;
        }
    }
    mysqli_query( $link, "SET foreign_key_checks = 1;");

脚本表格很好;但是,当我尝试在while循环中插入时,我收到以下错误

表'wolla.calendarworldholidays'不存在
但是表确实存在,因为创建它的代码执行正常,当我检查时,我可以在SQLPRO和phpmyadmin中看到它

$ holidayNames 是一个字符串数组 $ holidayDates 是一个日期数组

这两个都有值

请帮助,对此事的任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

使用撤消解决方案获得语法错误的原因是

Insert into

应该是

INSERT INTO

所有上限