Solved

PHP syntax mysql_query()

Posted on 2009-05-05
13
365 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 50

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 50

Expert Comment

by:Ryan Chong
ID: 24311614
shanikawm is well spotted for the quotes.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 50

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 50

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 50

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 50

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 50

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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