Link to home
Start Free TrialLog in
Avatar of thursdasy
thursdasyFlag for United States of America

asked on

PHP syntax mysql_query()

Hello I had a question on the correct syntax for the mysql_query() function I have the following code and I'm not sure how to properly execute it in my php script. Any examples or direction would be great!

Thanks

In the bellow example:

ID = int
note = string
IP = string?
delete = true or false (bool)
name = string


mysql_query("INSERT INTO notes 
(ID,  note, IP, delete, name) VALUES(IDGen(), $_POST["note"], getenv("REMOTE_ADDR"), false, $_POST["name"]) ") 
or die(mysql_error());

Open in new window

Avatar of shanikawm
shanikawm
Flag of Sri Lanka image

Try following code
mysql_query("INSERT INTO notes (ID,  note, IP, delete, name) VALUES(".IDGen().", '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."', false, '".$_POST["name"]."') ") or die(mysql_error());

Open in new window

Avatar of Ryan Chong
try:

mysql_query("INSERT INTO notes (ID,  note, IP, delete, name) VALUES(IDGen(), ".$_POST["note"].", ".getenv("REMOTE_ADDR").", false, ".$_POST["name"].") ")
or die(mysql_error());

?
shanikawm is well spotted for the quotes.
Avatar of thursdasy

ASKER

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 'delete, name) VALUES(1, '', '127.0.0.1', false, '')' at line 1

Any idea where this error is? Using the code from bellow. Which was posed by shanikawm.
mysql_query("INSERT INTO notes (ID,  note, IP, delete, name) VALUES(".IDGen().", '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."', false, '".$_POST["name"]."') ") or die(mysql_error());

Open in new window

try this instead:

mysql_query("INSERT INTO notes (ID,  note, IP, delete, name) VALUES(".IDGen().", '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."', 0, '".$_POST["name"]."') ") or die(mysql_error());


where use 0 for false, use 1 for true for your boolean/bit field.
so generally use this:

mysql_query("INSERT INTO notes (ID,  note, IP, delete, name) VALUES(".IDGen().", '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."', 0, '".$_POST["name"]."') ") or die(mysql_error());
There still seems to be something wrong =S

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 'delete, name) VALUES(1, '', '127.0.0.1', 0, '')' at line 1
what's the data type for your fields (ID,  note, IP, delete, name) ?
id is grab from anoher mysql table its a varchar(300) as you can see currently it's just one.
note - this is a random string submitted by the user and is stored in a mysql database as a text
IP - is the iP address of a user
delete - this is just false for now again stored in a mysql db under a boolean column
name - this is just the name submited by the user, i haven't created this for now so its just blank. But later it will be a text stored in a mysql db
so if your id is a varchar, try this instead:

mysql_query("INSERT INTO notes (ID,  note, IP, delete, name) VALUES('".IDGen()."', '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."', 0, '".$_POST["name"]."') ") or die(mysql_error());


basically, you need to put quotes for char fields' value.
Still nothing, there would be a different error if we were inserting the wrong data into the table. it has to do with the use of the ' ' or the " "
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore 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
Thank you very much that worked! Argh I should have noticed that! Late here -_-

Thanks so much for your time.