-1

这是代码:

    <html>

    <body>

    <?php
    $con = mysql_connect("localhost","moviek5q_arka","******");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    $pizza  = "Age
    Anger
    Architecture
    Art 
    Beauty
    Birthday
    Business
    Car
    Change
    Computers
    Dad
    Dating
    Death
    Design
    Diet
    Dreams
    Education
    Environmental
    Equality
    Experience
    Faith
    Family
    Finance
    Fitness
    Food
    Forgiveness
    Friendship 
    Funny
    Gardening
    Government
    Graduation
    Happiness
    Health
    History
    Home
    Humor
    Imagination
    Inspirational
    Intelligence
    Leadership
    Legal
    Life 
    Love 
    Marriage
    Medical
    Men
    Mom
    Money
    Motivational 
    Movies
    Music
    Nature
    Parenting
    Patriotism
    Peace
    Pet
    Poetry
    Politics
    Power
    Religion
    Science
    Society
    Sports
    Success 
    Technology
    Teen
    Time
    Travel
    Trust
    War
    Wedding
    Wisdom 
    Women
    Work
    ";
    $pieces = explode("\n", $pizza);
    $i=0;

    $a=mysql_select_db("moviek5q_Quote", $con);
    if(!$a) echo"Error";

    for($i=8;$i<75;$i++) {


    $sql=mysql_query("INSERT INTO moviek5q_Quote.Categories (Key, Name)
    VALUES
    (NULL,'$pieces[$i]')");
    if(!$sql) {die('Could not connect: ' . mysql_error());}

    }

    mysql_close($con);

    echo "done";
    ?>

    </body>

</html>

但是运行脚本会出现错误:

无法连接:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'Key, Name) VALUES (NULL,'Change')' 附近使用正确的语法

4

3 回答 3

4

Key是一个保留关键字,这就是它崩溃的原因。更改列名或将其放在引号 (`) 中。

例如

$sql = mysql_query( "INSERT INTO moviek5q_Quote.Categories (`Key`, Name)
VALUES
(NULL, '$pieces[$i]')" );

有关保留关键字的完整列表,请查看http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

于 2012-12-18T08:19:11.477 回答
2

您的字段名称Key保留字,因此查询将失败。您可以使用反引号来更正它:

INSERT INTO moviek5q_Quote.Categories (`Key`, Name)
于 2012-12-18T08:20:58.300 回答
2

只是一个提示,尝试使用 PDO 或 MySQLi,mysql_代码现在已被弃用,将来可能无法使用。

于 2012-12-18T08:27:31.213 回答