Jump to content

a little help


journeyman73

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.