Solved

How to alter table with php

Posted on 2007-11-29
6
1,385 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
[X]
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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Cornelia Yoder
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 i…
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…

707 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