whats wrong in this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
naeem1984
Forum Newbie
Posts: 10
Joined: Thu Feb 26, 2009 3:55 am

whats wrong in this code

Post by naeem1984 »

Code: Select all

 
$query = "insert into client set name='$_POST[name]', email='$_POST[email]', address='$_POST[address]',
  web_programming='1', 
  desktop_programming='2',
  hosting/registration='3' ";
 
this code could not insert data in database.

if we write the following code it works.

Code: Select all

 
$query = "insert into client set name='$_POST[name]', email='$_POST[email]', address='$_POST[address]',
 
but when we write full code it not works.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: whats wrong in this code

Post by jayshields »

It's not really a good idea to put forward slashes in field names, but if it actually works, then it should work fine in your query if you use backticks. Also, you should be putting string array indices in single quotes. Change your initial query to this:

Code: Select all

 
$query = "INSERT INTO `client` SET `name`= '{$_POST['name']}', `email` = '{$_POST['email']}', `address` = '{$_POST['address']}', `web_programming` = '1', `desktop_programming` = '2', `hosting/registration` = '3'";
 
I assume all your fields are strings, because the last 3 values should be without single quotes if they are to be taken as integers. I also advise you to use the original INSERT syntax, it makes more sense than to use this syntax which looks like an UPDATE query. You can see the original INSERT syntax in the first example in the manual.
Post Reply