douzhi3586 2014-04-20 22:33
浏览 37
已采纳

PHP / MySQL使用数组添加数据

I have written some PHP that is supposed to add data to a database through a text file. The database is connection properly but it is not executing the task. here is my code:

{ //Connect and test MySQL and specific DB (return $dbSuccess = T/F)

$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>";
        $dbSuccess = true;
    }
} else {
    echo "mySQL connection FAILED<br><br>";
    $dbSuccess = false;
}
}

// 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


    // Read CSV data file

    $file = fopen("datafile.txt", "r"); // Open the 'datafile' for 'r'eading

    $i = 0;

    while (!feof($file)) { // While NOT the End Of File
        $thisLine = fgets($file); // Gets the next line from "datafile"
        $personData[$i] = explode(",", $thisLine); // Sets
                                                           // $personData[$i] = array( $thisLine );
                                                        // Whatever's in $thisLine separated by commas.
        $i++;                                           //Increment $i
    }

    fclose($file);                                      // Close the file

    $numRows = sizeof($personData);

}

{ // SQL statement with ARRAYS

    // Fieldnames part in 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;

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

        if ($indx < ($numRows - 1)) {
            $person_SQLinsert .= ", ";
        }

        $indx++;

    }

} 

{ // 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)

?>

As the code closer to the bottom of the script makes it echo out "FAILED" if it wasn't able to do the function properly, all I get is

DB connection SUCCESSFUL

SQL: INSERT INTO tPerson ( Salutation, FirstName, LastName, CompanyID ) VALUES ( Mr, Mike, Freighn, 4 ), ( Mrs, Kathie, Arnott, 2 ), ( Ms, Zeta, Flowers, 1 ), ( M, Guy, Donnet, 3 ), ( Mrs, Harriet, Hennesey, 3 ), ( Dr, George, Terry, 2 ), ( Sir, Geoffrey, Paul, 1 ), ( Mrs, Laura, Winalott, 0 ), ( Mr, Peter, Bellows, 1 )

FAILED.

Please reply with any ideas on how I can fix this error as it has stumped me.

  • 写回答

1条回答 默认 最新

  • dt2015 2014-04-20 22:35
    关注

    You need quotes!

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

    Mind that this breaks, if a field contains a single quote - escape accordingly!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算