Avatar of qeng
qeng
 asked on

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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
// 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.";
?>

</body>
</html>

Open in new window

MySQL ServerPHP

Avatar of undefined
Last Comment
qeng

8/22/2022 - Mon
Hugh McCurdy

Try not repeating VALUES

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
ASKER CERTIFIED SOLUTION
Hugh McCurdy

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
qeng

ASKER
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).
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck