0

My MYSQL query isn't inserting properly via a form I had made. The query:

mysql_query("INSERT INTO Servers(ip, desc, type, title)
    VALUES($ip, $desc, $type, $title)"
) or die(mysql_error());

The MYSQL table is as follows:

CREATE TABLE IF NOT EXISTS Servers(
  id bigint(20) NOT NULL AUTO_INCREMENT,
  ip varchar(50) NOT NULL,
  desc text NOT NULL,
  type varchar(15) NOT NULL,
  title varchar(50) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3;

I already have two pre-made rows in the table. The form code:

<form action="" method="post">
  Title: <input type="text" name="title" /><br />
  IP: <input type="text" name="ip" /><br />
  Description:<br />
  <textarea name="desc"></textarea><br />
  Type: <input type="text" name="type" /><br />
  <input type="submit" name="submit" value="Submit server!" />
</form>

When I submit a server, I get the following error:

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 'desc, type,
title) VALUES(a, a, a, a)' at line 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Adam M
  • 113
  • 3
  • 13
  • Btw, you don't need to swap out angle brackets for square ones. Just prefix your html code block with four spaces, and ensure it has a preceding and following carriage return. – halfer Jan 29 '12 at 11:56

3 Answers3

3

Try this

mysql_query("INSERT INTO `Servers` (`ip`, `desc`, `type`, `title`) VALUES('".$ip."', '".$desc."', '".$type."', '".$title."')") or die(mysql_error()); 

desc is a reserved keyword of MySql, so it cannot be used without backticks.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gaurav
  • 28,447
  • 8
  • 50
  • 80
1

You should use quotes when inserting string values:

mysql_query("INSERT INTO Servers(ip, desc, type, title) VALUES('$ip' , '$desc', '$type', '$title')") or die(mysql_error()); 
Roel
  • 316
  • 1
  • 4
0

desc is a reserved work and you have to use ``. When your field is a (var)char, not a number, you have to enclose it in quotes in your script.

makriria
  • 383
  • 1
  • 9