Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert into syntax error

Status
Not open for further replies.

tsetsuit

Programmer
Mar 25, 2009
11
US
having a bit of an issue inserting some data into one of my mysql tables using php post from a html site. Keeps getting an error msg stating

"Error: 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 'table,type,email) VALUES ('0','Jan','1','2009','v','c','xz','Maintenance','z')' at line 1 "

after i run the submit. I don't see any problems in code and if I edit the html page and php page to post into a different database only using 3 text boxes it seems to work fine.
Any advice would be helpfull thank you.

Test2.html page
Code:
<html>
<head>
</head>
<body>

<form action='insert.php' method='post'>
	<p style='margin-bottom:0;'>Date</p>
		<select name='mon'>
			<option value='Jan'>Jan</option>
			<option value='Feb'>Feb</option>
			<option value='Mar'>Mar</option>
			<option value='Apr'>Apr</option>
			<option value='May'>May</option>
			<option value='June'>June</option>
			<option value='July'>July</option>
			<option value='Aug'>Aug</option>
			<option value='Sept'>Sept</option>
			<option value='Oct'>Oct</option>
			<option value='Nov'>Nov</option>
			<option value='Dec'>Dec</option>
		</select>

		 <select name='dat'> 
			 <option value='1'>1</option>
			 <option value='2'>2</option>
			 <option value='3'>3</option>
			 <option value='4'>4</option>
			 <option value='5'>5</option>
			 <option value='6'>6</option>
			 <option value='7'>7</option>
			 <option value='8'>8</option>
			 <option value='9'>9</option>
			 <option value='10'>10</option>
			 <option value='11'>11</option>
			 <option value='12'>12</option>
			 <option value='13'>13</option>
			 <option value='14'>14</option>
			 <option value='15'>15</option>
			 <option value='16'>16</option>
			 <option value='17'>17</option>
			 <option value='18'>18</option>
			 <option value='19'>19</option>
			 <option value='20'>20</option>
			 <option value='21'>21</option>
			 <option value='22'>22</option>
			 <option value='23'>23</option>
			 <option value='24'>24</option>
			 <option value='25'>25</option>
			 <option value='26'>26</option>
			 <option value='27'>27</option>
			 <option value='28'>28</option>
			 <option value='29'>29</option>
			 <option value='30'>30</option>
			 <option value='31'>31</option>
		 </select>

		 <select name='yea'> 
			 <option value='2009'>2009</option>
			 <option value='2010'>2010</option>
			 <option value='2011'>2011</option>
		 </select>

	<br/><br/>

	<p style='margin-bottom:0;'>Type</p>
		 <select name='typ'> 
			 <option value=''></option>
			 <option value='Maintenance'>Maintenance</option>
			 <option value='Meeting'>Meeting</option>
			 <option value='other'>other</option>
		 </select>

	<p style='margin-bottom:0;'>Employee Id</p>
		<input type='text' name='empid' /> <br/>
	<p style='margin-bottom:0;'>Subject</p>
		<input type='text' name='sub' /><br/>
	<p style='margin-bottom:0;'>posting info</p>
		<input type='text' name='tabs' /><br/>
	
	<p style='margin-bottom:0;'>Notify E-mails</p>
		<input type='text' name='ema' />
<input type='submit' />
</form>



</body>
</html>

insert.php page
Code:
<?php
include 'connection.php';

$sql="INSERT INTO postings (month,date,year,user_id,subject,table,type,email)
VALUES
('$_POST[mon]','$_POST[dat]','$_POST[yea]','$_POST[empid]','$_POST[sub]','$_POST[tabs]','$_POST[typ]','$_POST[ema]');";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

I know it looks plain and simple right now but want to get it working before i start putting the css and graphics into it.
 
oh and sorry here is my connection string forgot i had it on another page

Code:
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("calendar", $con);
and yes i changed my password to the word 'password' for this post and it is correct in my document.
 
To be exact, most of the columns you use are reserved words: date, year, month. quote them as well.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
I would just say they (month, date, year) are dangerously chosen, in particularly, date. It is reserved but tolerated. All of them can be compromising any time if it is ever to port to other databases in general. Furthermore, they create a cloud of doubt every time one writes queries. In fact, it is better not to choose any of them (including 'table' of course) as the lexical space is that big beyond imagination.
 
great thank you. I knew it could conflict from a $_post[name] but i didn't know the row names could conflict. Thank you very much for help.
 
pls note the superglobals (and all variables in php) are case sensitive. reference must be to $_POST to access the superglobal (unless you have set up an alias)
Code:
$_post &= $_POST
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top