Hi,
I am trying to set up an array that will insert data in an existing dbtable that I created. The data should be read from the data file in the same folder but a for some reason it fails. I have entered data into the table before but not using the data file.
I cannot see why it is not working now. The only thing that has changed is the setup array of data rows.
Can someone help me?
This insert works:
<?php /* { // Connect and Test MySQL and specific DB (return $dbSuccess = T/F) $hostname = "localhost"; $username = "root"; $password = "yourpasswordhere"; $databaseName = "alphacrm"; $dbConnected = @mysql_connect($hostname, $username, $password); $dbSelected = @mysql_select_db($databaseName,$dbConnected); $dbSuccess = true; if ($dbConnected) { if (!$dbSelected) { echo "DB connection FAILED"; $dbSuccess = false; } } else { echo "MySQL connection FAILED
"; $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 $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; 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 "SQL:
"; echo $person_SQLinsert."
"; if (mysql_query($person_SQLinsert)) { echo "was SUCCESSFUL.
"; } else { echo "FAILED.
"; } } } // END ($dbSuccess) ?>
This fails:
<?php /* { // Connect and Test MySQL and specific DB (return $dbSuccess = T/F) $hostname = "localhost"; $username = "root"; $password = "yourpasswordhere"; $databaseName = "alphacrm"; $dbConnected = @mysql_connect($hostname, $username, $password); $dbSelected = @mysql_select_db($databaseName,$dbConnected); $dbSuccess = true; if ($dbConnected) { if (!$dbSelected) { echo "DB connection FAILED"; $dbSuccess = false; } } else { echo "MySQL connection FAILED
"; $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", "r"); // open the file '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 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; 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 } // END ($dbSuccess) ?>