PDA

View Full Version : Trouble filling table from function


rbpd5015
11-26-2005, 12:01 AM
I am trying to fill the table MYSQL_GBGPASSING, but no matter what I do, it wont fill. Do you see something wrong here?

Thanks in advance
Matt


function sql_insertPlayerGbgPassingStats($id, $year, $wk, $table)
{
$db_table = MYSQL_PASSING;
$weeklydb_table = MYSQL_GBGPASSING;

$cmp = $table[2]; $att = $table[3]; $yds = $table[4]; $sack = $table[7]; $td = $table[8];
$int = $table[9]; $long = $table[10]; $wk = $_POST["wk"];

$query = "INSERT INTO $weeklydb_table "
."(`id`, `year`, wk, cmp, att, yds, sack, td, `int`, `long`) "
."SELECT (id, `year`, $wk, $cmp-cmp, $att-att, $yds-yds, $sack-sack, $td-td, $int-`int`, IF(`long`>$long,`long`,$long) "
."FROM $db_table "
. "WHERE id = $id AND `year` = $year";
$result = @mysql_query($query);
if ($result == false)
return errorPrint("The following query in sql_insertPlayerWeeklyPassingStats($id, $year, $table); failed: <p><tt>$query</tt></p>");

return true;



}

vinyl-junkie
11-26-2005, 01:30 AM
Are you getting any kind of error message?

rbpd5015
11-26-2005, 05:47 AM
I have wrote a select statement and I am having problems using it.

The following query in sql_insertPlayerWeeklyPassingStats(21088, 2005, Array); failed:

INSERT INTO ind_gbgpassing (`id`, `year`, wk, cmp, att, yds, sack, td, `int`, `long`) SELECT id, `year`, 1, 21-cmp, 31-att, 311-yds, 1-sack, 3-td, 1-`int`, IF(`long`>65,`long`,65 FROM ind_passing WHERE id = 21088 AND `year` = 2005


MySQL said: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ind_passing WHERE id = 21088 AND `year` = 2005' at line 1

[b]

below is the code I use, do you guys understand why this is messing up. I am 99% sure it has to do with lines being produced like this
[b]21-cmp, 31-att, 311-yds, 1-sack, 3-td, 1-`int`
Any help would sure be appreciated

function sql_insertPlayerGbgPassingStats($id, $year, $table)
{
$db_table = MYSQL_PASSING;
$weeklydb_table = MYSQL_GBGPASSING;

$cmp = $table[2]; $att = $table[3]; $yds = $table[4]; $sack = $table[7]; $td = $table[8];
$int = $table[9]; $long = $table[10]; $wk = $_POST["wk"];

$query = "INSERT INTO $weeklydb_table "
."(`id`, `year`, wk, cmp, att, yds, sack, td, `int`, `long`) "
."SELECT id, `year`, $wk, $cmp-cmp, $att-att, $yds-yds, $sack-sack, $td-td, $int-`int`, IF(`long`>$long,`long`,$long "
."FROM $db_table "
."WHERE id = $id AND `year` = $year ";
$result = @mysql_query($query);
echo $query;
if ($result == false)
return errorPrint("The following query in sql_insertPlayerWeeklyPassingStats($id, $year, $table); failed: <p><tt>$query</tt></p>");

return true;



}

vinyl-junkie
11-26-2005, 06:07 AM
Change this phrase in your query:

AND `year` = 2005
to this:

AND year = 2005

rbpd5015
11-26-2005, 07:34 AM
I did that and I get the same error.

What can I possible be doing wrong I just dont get it.

MAtt


The following query in sql_insertPlayerWeeklyPassingStats(22845, 2005, Array); failed:

INSERT INTO ind_gbgpassing (`id`, `year`, wk, cmp, att, yds, sack, td, `int`, `long`) VALUES (id, `year`, 1, 21-cmp, 31-att, 311-yds, 1-sack, 3-td, 1-`int`, IF(`long`>65,`long`,65) FROM ind_passing WHERE id = 22845 AND year = 2005

vinyl-junkie
11-26-2005, 08:12 AM
Depending on what language you're using for your INSERT, you may need to do something like this:

INSERT INTO ind_gbgpassing (`id`, `year`, wk, cmp, att, yds, sack, td, `int`, `long`) VALUES (id, `year`, 1, 21-cmp, 31-att, 311-yds, 1-sack, 3-td, 1-`int`, IF(`long`>65,`long`,65) FROM ind_passing WHERE id = 22845 AND year = #2005#

rbpd5015
11-26-2005, 08:33 AM
see I dont want this

21-cmp, 31-att, 311-yds, 1-sack, 3-td

thats the problem

look here
Testing it more the problem has to be here


."SELECT id, `year`, $wk, $cmp-cmp, $att-att, $yds-yds, $sack-sack, $td-td, $int-`int`, IF(`long`>$long,`long`,$long "


cause I can change it to this and it works fine


."SELECT id, `year`, $wk, $cmp, $att, $yds, $sack, $td, $int-, $Long "


The only problem is it imports the season stats into the week stats so It looks like this
lets say my week by week stats were this just cmp and att
12 24
13 20
21 30
it would look like this
ID YEAR WK CMP ATT blah blah
333 2005 1 12 24
333 2005 2 25 44
333 2005 3 46 74

thats why I tried to do this I tried to do this $cmp-cmp, $att-att.


Matt

raf
11-27-2005, 09:46 AM
if you get such a MySQL-error, then you always need to look at the syntax right before the quoted one. In your case 'FROM etc'
You'll then see you're missing a ')' for the if() construct

INSERT INTO ind_gbgpassing (`id`, `year`, wk, cmp, att, yds, sack, td, `int`, `long`) SELECT id, `year`, 1, 21-cmp, 31-att, 311-yds, 1-sack, 3-td, 1-`int`, IF(`long`>65,`long`,65 FROM ind_passing WHERE id = 21088 AND `year` = 2005

should be

INSERT INTO ind_gbgpassing (`id`, `year`, wk, cmp, att, yds, sack, td, `int`, `long`) SELECT id, `year`, 1, 21-cmp, 31-att, 311-yds, 1-sack, 3-td, 1-`int`, IF(`long`>65,`long`,65) FROM ind_passing WHERE id = 21088 AND `year` = 2005