本文介绍了插入数据与数组表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的阵列将数据插入到一个名为tPerson表。如果脚本成功地在它的作用,它会回应:

I am using arrays to insert data into a table named tPerson. If the script succeeded in its role, it would echo:

SQL:
  INSERT INTO tPerson(称呼,名字,姓氏,CompanyID)VALUES(先生,莫里斯,麻雀,4)(夫人玛丽,哈斯利特,2)(MS,吉尔,亨纳,1)

是成功的。

它不会在它的工作取得成功,并呼应了:

It doesn't succeed in its job and echoes out:

SQL:
  INSERT INTO tPerson(称呼,名字,姓氏,CompanyID)VALUES(先生,莫里斯,麻雀,4)(夫人玛丽,哈斯利特,2)(MS,吉尔,亨纳,1)

失败。

我的code是如下:

$hostname = "localhost";
$username = "root";
$password = "";

$databaseName = "alphacrm";

$dbConnected = @mysql_connect($hostname, $username, $password);
$dbSelected = @mysql_select_db($databaseName, $dbConnected);

$dbSuccess = true;
if ($dbConnected) {
    if (!$dbSelected) {
        echo "DB connection FAILED<br><br>";
        $dbSuccess = false;
    } else {
        echo "DB connection SUCCESSFUL<br><br>";
    }
} else {
    echo "MySql connection FAILED<br><br>";
}
}

// Execute code ONLY if connections were successful

if ($dbSuccess) {

{ // setup ARRAY of field names

    $personField = array(
        "Salutation" => "Salutation",
        "FirstName" => "FirstName",
        "LastName" => "LastName",
        "CompanyID" => "CompanyID"
    );
}

{ // setup ARRAY of data ROWS

    $personData[0] = array("Mr","Morris","Sparrow","4");
    $personData[1] = array("Mrs","Mary","Haslett","2");
    $personData[2] = array("Ms","Gill","Hennesey","1");

    $numRows = sizeof($personData);
}

{ // SQL statement with ARRAYS

    //Fieldnames part of INSERT statement

    $person_SQLinsert = "INSERT INTO tPerson (
        ".$personField["Salutation"].",
        ".$personField["FirstName"].",
        ".$personField["LastName"].",
        ".$personField["CompanyID"]."
        )";

// VALUES part of INSERT statement

$person_SQLinsert .= "VALUES ";

$indx = 0;
$person_SQLinsert .= "(
    ".$personData[$indx][0].",
    ".$personData[$indx][1].",
    ".$personData[$indx][2].",
    ".$personData[$indx][3]."
    )";

$indx++;
$person_SQLinsert .= "(
    ".$personData[$indx][0].",
    ".$personData[$indx][1].",
    ".$personData[$indx][2].",
    ".$personData[$indx][3]."
    )";

$indx++;
$person_SQLinsert .= "(
    ".$personData[$indx][0].",
    ".$personData[$indx][1].",
    ".$personData[$indx][2].",
    ".$personData[$indx][3]."
    )";

}

{ // Echo and execute the SQL and test for success

    echo "<strong><u>SQL:<br></u></strong>";
    echo $person_SQLinsert."<br><br>";

    if (mysql_query($person_SQLinsert)) {
        echo "was SUCCESSFUL.<br<br>";
    } else {
        echo "FAILED.<br><br>";
    }
}
} // END ($dbSuccess)

?>

所有的帮助都将是AP preciated,因为我很为难,为什么我的脚本不能正常工作。

All help at all will be appreciated as I am quite stumped as to why my script is not working.

推荐答案

就像用于@mysql_query代替本该的mysql_query @mysql_select_db。
还检查了你的mysql连接和数据库连接成功。

Just like that @mysql_select_db used for @mysql_query instead of this mysql_query.Also checked your mysql connection and database connection successful.

您为多行的查询必须在此格式
INSERT INTO表(列1,列2)VALUES(值1,值2),(值1,值);

Your query for multiple row must be in this format"INSERT INTO Table ( Column1, Column2 ) VALUES( Value1, Value2 ), ( Value1, Value2 );"

打印您的查询,使用此语法检查。

Print your query and check it with this syntax.

这篇关于插入数据与数组表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 18:20