I have been working on a project for about a week now and today i seem to of hit a massive brick wall. whilst working on writing some data to a database, something which is usually second nature, i keep getting syntax errors. Now i have gone over the code so much that the text is blending together and my head feels like an elephant is sat on it, with no results.

For test purposes i trimmed the code down to just write data to the first two fields of the database, ID & components(see code below). That was a success. However when i coded all 30 of the fields i began to get my errors.

Is there a limit as to how many records you can add to a mysql database at one time? Or is my eyesight finally going? :icon_cheesygrin:

I have enclosed the full code plus the error message i receive in the hope that someone might just say "aaaaah you missed out an apostrophe". Or something to that effect.

Thanks in advance.


My error message.

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 'case, tempco, location, manufacturer, date, datasheet, reclaimed, user, code, ro' at line 1

My code.
All session variables contain at least 3 characters at all times.
The 'name' & 'date' fields will eventually use variables. i have just nulled them for now.
The ID field auto increments.

<?php
session_start();

// include DB details. In my case  "localhost", "root", "", "_components" 
require_once("content_db.php");

//connect to database
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($sql_connect_error);
mysql_select_db($dbname);

$Query = "INSERT INTO _components (ID, category, quantity, description, value, volt, current, tolerance, power, wafer, t_type, package, case, tempco, location, manufacturer, date, datasheet, reclaimed, user, code, rohs, mount, material, esr, dimensions, custom_code, track_taper, turns, gang) VALUES ('NULL', '".$_SESSION['category']."', '".$_SESSION['quantity']."', '".$_SESSION['description']."', '".$_SESSION['value']."', '".$_SESSION['volt']."', '".$_SESSION['current']."', '".$_SESSION['tolerance']."', '".$_SESSION['power']."', '".$_SESSION['wafer']."', '".$_SESSION['t_type']."', '".$_SESSION['package']."', '".$_SESSION['case']."', '".$_SESSION['tempco']."', '".$_SESSION['location']."', '".$_SESSION['manufacturer']."', 'NULL', '".$_SESSION['datasheet']."', '".$_SESSION['reclaimed']."', 'NULL', '".$_SESSION['code']."', '".$_SESSION['rohs']."', '".$_SESSION['mount']."', '".$_SESSION['material']."', '".$_SESSION['esr']."', '".$_SESSION['dimensions']."', '".$_SESSION['custom_code']."', '".$_SESSION['track_taper']."', '".$_SESSION['turns']."', '".$_SESSION['gang']."')";									
mysql_query($Query) or die(mysql_error());
mysql_close($conn);
?>
Member Avatar for diafol

WHy don't you set up a key-value array and loop it until you hit an error?

Anyway use NULL instead of 'NULL'

You also seem to be using reserved words as fieldnames and you don't backtick them. do this, e.g.

...,`date`,...

If you have a real problem with this, try the following, moving the /* down every time to see when the error (if it exists) appears:

$r = array(
	'ID' => NULL,
	'category' => 'category',
/*	'quantity' => 'quantity',
	'description' => 'description',
	'value' => 'value',
	'volt' => 'volt',
	'current' => 'current',
	'tolerance' => 'tolerance',
	'power' => 'power',
	'wafer' => 'wafer',
	't_type' => 't_type',
	'package' => 'package', 
	'case' => 'case', 
	'tempco' => 'tempco', 
	'location' => 'location', 
	'manufacturer' => 'manufacturer', 
	'date' => NULL, 
	'datasheet' => 'datasheet', 
	'reclaimed' => 'reclaimed', 
	'user'=>NULL, 
	'code' => 'code', 
	'rohs' => 'rohs', 
	'mount' => 'mount', 
	'material' => 'material', 
	'esr' => 'esr', 
	'dimensions' => 'dimensions', 
	'custom_code' => 'custom_code', 
	'track_taper' => 'track_taper', 
	'turns' => 'turns', 
	'gang' => 'gang' */
);

foreach($r as $fieldname => $value){
	$fields[] = "`$fieldname`"; 
	$values[] = (is_null($value)) ? 'NULL' : "'" . $_SESSION[$value] . "'";	
}
$fieldlist = implode(",",$fields);
$valuelist = implode(",",$values);
$sql = "INSERT INTO _components ($fieldlist)VALUES($valuelist)";
echo $sql;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.