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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。