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
Solved

How to alter table with php

Posted on 2007-11-29
6
1,378 Views
Last Modified: 2013-12-12
What's wrong with this:

$profilename = 'photographers';
$name = 'Gender';
$type = 'varchr(255)';

$result = mysql_query("ALTER TABLE '$profilename' ADD COLUMN '$name' '$type'") or die(mysql_error());

It says:

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 ''' ADD COLUMN 'Gender' 'varchar(255)'' at line 1
0
Comment
Question by:jtcy
6 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20372906

$profilename = 'photographers';
$name = 'Gender';
$type = 'varchar(255)';
 
$result = mysql_query("ALTER TABLE `$profilename` ADD COLUMN `$name` $type") or die(mysql_error());

Open in new window

0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 350 total points
ID: 20372913
use backticks `` on entities (tables, columns) not single columns '
Also the datatype is varchar not varchr
0
 

Author Comment

by:jtcy
ID: 20372980
Whats wrong with this:

$result = mysql_query("INSERT INTO profile_fields (`id`, `name`, `profile_id`, `type_id`, `order`) VALUE
  ('0', '$name', '$profileid', '$type', '$order'") or die(mysql_error());
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:yodercm
ID: 20372981
I don't think you need the ticks, just fix the varchr/varchar problem.

$type = 'varchar(255)';
0
 
LVL 3

Assisted Solution

by:BioI
BioI earned 150 total points
ID: 20373014
the problem with your insert-statement is that mix up your parentheses and quotes
copy-past this line:


$result = mysql_query("INSERT INTO profile_fields (`id`, `name`, `profile_id`, `type_id`, `order`) VALUES  ('0', '$name', '$profileid', '$type', '$order')") or die(mysql_error());

Open in new window

0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 350 total points
ID: 20373109
You should use backtcks especially when executing alter statements dynamically ...

for example if you use

$name = 'sort'; which is a reserved word it will fail without the backticks.

on this query

$result = mysql_query("INSERT INTO profile_fields (`id`, `name`, `profile_id`, `type_id`, `order`) VALUE
  ('0', '$name', '$profileid', '$type', '$order'") or die(mysql_error());

as BioI suggested above you are missing a closing ) and using value instead of values

Also if your id field is autoincremented PK then you should use

$result = mysql_query("INSERT INTO profile_fields (`name`, `profile_id`, `type_id`, `order`) VALUES
  ('$name', '$profileid', '$type', '$order')") or die(mysql_error());

 
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fulfillment API php code sample 1 58
how to call a remote wordpress to use his  content in a loop 3 33
Currency in SQL? 2 30
Reference key in foreach loop 4 18
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

856 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