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 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)