[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

MySQL/PHP Syntax Error

Running  PHP/5.3.1 MySQL client version: 5.1.41  under XAMPP 1.7.3:

Can't figure out what is wrong with the syntax in the attached code (adapted from an example in a PHP book).  Code is attached.

Error (pasted from browser output):

connecting to MySQL
inserting data into table: playeridlist
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 'VALUES (2, 'Fred', 'a', 'Flintstone'), VALUES (3, 'Donald', 'a', 'Duck')' at line 1


Any ideas?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

// connect to:  MySQL
echo "connecting to MySQL";
echo "<br>";
$connect = mysql_connect ("localhost", "root", "") 
	or die ("wasn't able to connect to MySQL");

// ensure "players" database is current one
mysql_select_db ("players2");

// insert data into table "playeridlist"
echo "inserting data into table:  playeridlist";
echo "<br>";
$insert = "INSERT INTO playeridlist (player_id, player_firstname, player_middleinitial, player_lastname) " .
			"VALUES (1, 'John', 'a', 'Smith'), " .
			"VALUES (2, 'Fred', 'a', 'Flintstone'), " .
			"VALUES (3, 'Donald', 'a', 'Duck')";

$results = mysql_query($insert)
	or die (mysql_error());

// insert data into table "position"
echo "inserting data into table:  position";
echo "<br>";
$posns = "INSERT INTO position (position_id, position_labelabbr) " .
			"VALUES (1, 'DB'), " .
			"VALUES (2, 'WR'), " .
			"VALUES (3, 'RB')";

$results = mysql_query($posns)
	or die (mysql_error());

echo "Players data successfully entered.";


Open in new window

  • 2
1 Solution
Hugh McCurdyCommented:
Try not repeating VALUES

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Hugh McCurdyCommented:
$insert = "INSERT INTO playeridlist (player_id, player_firstname, player_middleinitial, player_lastname) " .
                  "VALUES (1, 'John', 'a', 'Smith'), " .
                  "(2, 'Fred', 'a', 'Flintstone'), " .
                  "(3, 'Donald', 'a', 'Duck')";

I'm not sure this is the problem but it's my first guess.
qengAuthor Commented:
Will try but this will get difficult to manage.  This is just an example I'm troubleshooting.  I will have approximately 40 lines of values to add once this works (I'll ultimately do this more elegantly but have to get something out for now).

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now