thursdasy
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
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());
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());
?
mysql_query("INSERT INTO notes (ID, note, IP, delete, name) VALUES(IDGen(), ".$_POST["note"].", ".getenv("REMOTE_ADDR").",
or die(mysql_error());
?
shanikawm is well spotted for the quotes.
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.
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());
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.
mysql_query("INSERT INTO notes (ID, note, IP, delete, name) VALUES(".IDGen().", '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."
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());
mysql_query("INSERT INTO notes (ID, note, IP, delete, name) VALUES(".IDGen().", '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."
ASKER
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
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) ?
ASKER
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
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.
mysql_query("INSERT INTO notes (ID, note, IP, delete, name) VALUES('".IDGen()."', '".$_POST["note"]."', '".getenv("REMOTE_ADDR")."
basically, you need to put quotes for char fields' value.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much that worked! Argh I should have noticed that! Late here -_-
Thanks so much for your time.
Thanks so much for your time.
Open in new window