Solved

How to alter table with php

Posted on 2007-11-29
6
1,369 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now