journeyman73 Posted May 29, 2008 Share Posted May 29, 2008 Hi, I would like to change this code so that i only 1 report that it is successful or a failure i have tried by moving the echo out of the query, but then i only get one record inserted any ideas on how i can do it, also if possible how can i get it to report that the delete is successful as well as i get a query was empty error <?php { $deleteSQL = sprintf("DELETE FROM player_averages"); if(mysql_query($deleteSQ)) { echo "<span style=\"color: green;\">Player Averages Deleted</span>\r\n"; } else { echo "<span style=\"color: red;\">Failed!</span> Reason: ".mysql_error()."<br>\r\n"; } } // Establish database connection here mysql_select_db($database_torquay_skittles); // Data $file = '../uploads/ext005.csv'; $table = "player_averages"; // MySQL table to insert into $fields = "player_name, player_age, player_team, player_sex, player_games, player_total, player_average, player_high, player_low, player_rank"; $s = file_get_contents($file); $rs = "\n"; // Row seperator $cs = ","; // Column seperator $esc = "\\"; // Escape character $quot = "\""; // Column data encloser // Parse input $row = 0; $col = 0; $data = array(); $escape = false; $quote = false; for($i = 0; $i < strlen($s); $i++) { if(!empty($quot) && ($s{$i} == substr($quot, 0, 1)) && !$escape) { $quote = !$quote; } elseif(!empty($quot) && ($s{$i} == $esc) && !$escape) { $escape = true; } elseif(($s{$i} == $cs) && !$quote) { $col++; $escape = false; } elseif(($s{$i} == $rs) && !$quote) { $col = 0; $row++; $escape = false; } else { $data[$row][$col] .= $s{$i}; $escape = false; } } function prepare($v) { $v = is_array($v) ? array_map("prepare", $v) : addslashes(trim($v)); return $v; } // Insert the data foreach($data as $row) { $row = prepare($row); $query = "INSERT INTO ".$table." (".$fields.") VALUES ('".implode("', '", $row)."')"; if(mysql_query($query)) { echo "<span style=\"color: green;\">Player Averages Updated</span>\r\n"; } else { echo "<span style=\"color: red;\">Failed!</span> Reason: ".mysql_error()."<br>\r\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/107773-a-little-help/ Share on other sites More sharing options...
trq Posted May 29, 2008 Share Posted May 29, 2008 I would like to change this code so that i only 1 report that it is successful or a failure Sorry, but this sentence is nonsensical. Link to comment https://forums.phpfreaks.com/topic/107773-a-little-help/#findComment-552419 Share on other sites More sharing options...
journeyman73 Posted May 29, 2008 Author Share Posted May 29, 2008 at the moment it reports everytime a line is added to the db but i only want it to show it that it has finished Link to comment https://forums.phpfreaks.com/topic/107773-a-little-help/#findComment-552433 Share on other sites More sharing options...
journeyman73 Posted May 29, 2008 Author Share Posted May 29, 2008 i've changed the code a little anybody, any ideas on how to fix this <?php { mysql_query("TRUNCATE TABLE player_averages"); } // Establish database connection here mysql_select_db($database_torquay_skittles); // Data $file = '../uploads/ext005.csv'; $table = "player_averages"; // MySQL table to insert into $fields = "player_name, player_age, player_team, player_sex, player_games, player_total, player_average, player_high, player_low, player_rank"; $s = file_get_contents($file); $rs = "\n"; // Row seperator $cs = ","; // Column seperator $esc = "\\"; // Escape character $quot = "\""; // Column data encloser // Parse input $row = 0; $col = 0; $data = array(); $escape = false; $quote = false; for($i = 0; $i < strlen($s); $i++) { if(!empty($quot) && ($s{$i} == substr($quot, 0, 1)) && !$escape) { $quote = !$quote; } elseif(!empty($quot) && ($s{$i} == $esc) && !$escape) { $escape = true; } elseif(($s{$i} == $cs) && !$quote) { $col++; $escape = false; } elseif(($s{$i} == $rs) && !$quote) { $col = 0; $row++; $escape = false; } else { $data[$row][$col] .= $s{$i}; $escape = false; } } function prepare($v) { $v = is_array($v) ? array_map("prepare", $v) : addslashes(trim($v)); return $v; } // Insert the data foreach($data as $row) { $row = prepare($row); $query = "INSERT INTO ".$table." (".$fields.") VALUES ('".implode("', '", $row)."')"; if(mysql_query($query)) { echo "<span style=\"color: green;\">Player Averages Updated</span>\r\n"; } else { echo "<span style=\"color: red;\">Failed!</span> Reason: ".mysql_error()."<br>\r\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/107773-a-little-help/#findComment-552552 Share on other sites More sharing options...
BlueSkyIS Posted May 29, 2008 Share Posted May 29, 2008 move the echo out of the loop to after the loop. as long as anything is inside a loop, it's going to LOOP over and over. Link to comment https://forums.phpfreaks.com/topic/107773-a-little-help/#findComment-552560 Share on other sites More sharing options...
journeyman73 Posted May 29, 2008 Author Share Posted May 29, 2008 i have tried moving the echo out but i only get the last line of the CSV inserted into the DB <?php { mysql_query("TRUNCATE TABLE player_averages"); } // Establish database connection here mysql_select_db($database_torquay_skittles); // Data $file = '../uploads/ext005.csv'; $table = "player_averages"; // MySQL table to insert into $fields = "player_name, player_age, player_team, player_sex, player_games, player_total, player_average, player_high, player_low, player_rank"; $s = file_get_contents($file); $rs = "\n"; // Row seperator $cs = ","; // Column seperator $esc = "\\"; // Escape character $quot = "\""; // Column data encloser // Parse input $row = 0; $col = 0; $data = array(); $escape = false; $quote = false; for($i = 0; $i < strlen($s); $i++) { if(!empty($quot) && ($s{$i} == substr($quot, 0, 1)) && !$escape) { $quote = !$quote; } elseif(!empty($quot) && ($s{$i} == $esc) && !$escape) { $escape = true; } elseif(($s{$i} == $cs) && !$quote) { $col++; $escape = false; } elseif(($s{$i} == $rs) && !$quote) { $col = 0; $row++; $escape = false; } else { $data[$row][$col] .= $s{$i}; $escape = false; } } function prepare($v) { $v = is_array($v) ? array_map("prepare", $v) : addslashes(trim($v)); return $v; } // Insert the data foreach($data as $row) { $row = prepare($row); $query = "INSERT INTO ".$table." (".$fields.") VALUES ('".implode("', '", $row)."')"; } if(mysql_query($query)) { echo "<span style=\"color: green;\">Player Averages Updated</span>\r\n"; } else { echo "<span style=\"color: red;\">Failed!</span> Reason: ".mysql_error()."<br>\r\n"; } ?> Like so, so could somebody please point me in the right direction on how to fix this cheers Kevin Link to comment https://forums.phpfreaks.com/topic/107773-a-little-help/#findComment-552594 Share on other sites More sharing options...
journeyman73 Posted May 30, 2008 Author Share Posted May 30, 2008 bump anyone have any ideas on this??? Kevin Link to comment https://forums.phpfreaks.com/topic/107773-a-little-help/#findComment-553311 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.