Solved

PHP syntax mysql_query()

Posted on 2009-05-05
13
361 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:thursdasy
  • 7
  • 5
13 Comments
 
LVL 8

Expert Comment

by:shanikawm
Comment Utility
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

0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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());

?
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
shanikawm is well spotted for the quotes.
0
 
LVL 2

Author Comment

by:thursdasy
Comment Utility
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

0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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.
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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());
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 2

Author Comment

by:thursdasy
Comment Utility
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
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
what's the data type for your fields (ID,  note, IP, delete, name) ?
0
 
LVL 2

Author Comment

by:thursdasy
Comment Utility
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
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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.
0
 
LVL 2

Author Comment

by:thursdasy
Comment Utility
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 " "
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
delete is a reserved word, you have to use 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());
0
 
LVL 2

Author Comment

by:thursdasy
Comment Utility
Thank you very much that worked! Argh I should have noticed that! Late here -_-

Thanks so much for your time.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

12 Experts available now in Live!

Get 1:1 Help Now