[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to alter table with php

Posted on 2007-11-29
6
Medium Priority
?
1,396 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 1400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 600 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 1400 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

649 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