MySQL/PHP Syntax Error

Posted on 2011-10-09
Last Modified: 2012-06-27
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" "">
<html xmlns="">
<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

Question by:qeng
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    Try not repeating VALUES

    INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
    LVL 13

    Accepted Solution

    $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.

    Author Comment

    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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now