0

I have this line of sql which gives me a syntax error following the closing parenthesis of the select statement. I was under the belief that I could write the code this way. Is there some alternative?

The variables inside the select statement come from GET and the others come from POST. Any help is appreciated.

$sql2="INSERT INTO playerRegSeason 
       (playerID, year, teamID, gp, minutes, pts, oreb, dreb, reb, 
        asts, stl, blk, turnover, pf, fga, fgm, fta, ftm, tpa, tpm)
VALUES ((
       SELECT playerID FROM players WHERE firstname='$firstname' 
       AND lastname='$lastname' AND firstseason=$firstseason), $year, 
      '$team', $gp, $minutes, $pts, $oreb, $dreb, $reb, $asts, $stl, 
       $blk, $turnover, $pf, $fga, $fgm, $fta, $ftm, $tpa, $tpm)";

5 Answers 5

2

Don't use VALUES when you're inserting from a select.

INSERT INTO abc (foo, bar)
SELECT x, y FROM z
Sign up to request clarification or add additional context in comments.

Comments

0

Use this.

$sql2="INSERT INTO playerRegSeason 
       (playerID, year, teamID, gp, minutes, pts, oreb, dreb, reb, 
        asts, stl, blk, turnover, pf, fga, fgm, fta, ftm, tpa, tpm)

       SELECT playerID, $year, '$team', $gp, $minutes, $pts, $oreb, 
         $dreb, $reb, $asts, $stl, $blk, $turnover, $pf, $fga, $fgm, 
         $fta, $ftm, $tpa, $tpm FROM players WHERE firstname='$firstname' 
       AND lastname='$lastname' AND firstseason=$firstseason";

2 Comments

I get this error: Error: 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 'SELECT playerID FROM players WHERE firstname='' AND lastname='' AND firstseason=' at line 2
@user2884641: check the sample in sql fiddler sqlfiddle.com/#!2/f4ef5d/1 and sqlfiddle.com/#!2/ab89e/1
0
insert into table1 (field1, field2, ...)
select col1, col2, ...
from table2
where ....

Comments

0

The SQL statement you wrote is incorrect, you need to remove the values clause or you can refer Insert with select to learn how to write a insert with a select statement

Comments

0

Use this query instead of that one it's working

$sql2="INSERT INTO playerRegSeason (playerID, year, teamID, gp ,minutes, pts, oreb, dreb, reb, asts, stl, blk, turnover, pf, fga, fgm, fta, ftm, tpa, tpm)

    values(SELECT playerID,year, $team, $gp, $minutes, $pts, $oreb, $dreb, 
    $reb, $asts, $stl, $blk, $turnover, $pf, $fga, $fgm, $fta, $ftm, $tpa, $tpm
    WHERE firstname='$firstname' 
    AND lastname='$lastname' AND firstseason='$firstseason')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.