Solved

PHP syntax mysql_query()

Posted on 2009-05-05
13
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 8

Expert Comment

by:shanikawm
ID: 24311610
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 52

Expert Comment

by:Ryan Chong
ID: 24311611
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 52

Expert Comment

by:Ryan Chong
ID: 24311614
shanikawm is well spotted for the quotes.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:thursdasy
ID: 24322507
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 52

Expert Comment

by:Ryan Chong
ID: 24322559
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 52

Expert Comment

by:Ryan Chong
ID: 24322566
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
 
LVL 2

Author Comment

by:thursdasy
ID: 24322613
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 52

Expert Comment

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

Author Comment

by:thursdasy
ID: 24322699
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 52

Expert Comment

by:Ryan Chong
ID: 24322714
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
ID: 24322765
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 52

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 24322834
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
ID: 24323109
Thank you very much that worked! Argh I should have noticed that! Late here -_-

Thanks so much for your time.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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