Solved

PHP syntax mysql_query()

Posted on 2009-05-05
13
366 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
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 51

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 51

Expert Comment

by:Ryan Chong
ID: 24311614
shanikawm is well spotted for the quotes.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 51

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 51

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 51

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 51

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 51

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

860 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