Link to home
Start Free TrialLog in
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

Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Try not repeating VALUES

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
ASKER CERTIFIED SOLUTION
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qeng
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).