shotos Posted August 13, 2008 Share Posted August 13, 2008 hello, i just finished writing a script that inserts inputted data into one table and also adds a column to another table. Now, inserting into the first table works perfectly but the alter command produces this a this error: 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 'Tita add car varchar(50)' at line 1 Today is my second day figuring out whats wrong.........am all worn out. help is welcomed. This is the code snippet involved in writing into the database. include_once('mysql.php'); $title = $_SESSION['edit_title']; $db = &new Mysql(); $num_items = count($items); $id = 0; do { $element = $id + 1; $alignment = $id + 2; $label = $id + 3; $add = "INSERT INTO $title(check_item, checklist_element, alignment, labels) VALUES('$items[$id]','$items[$element]','$items[$alignment]','$items[$label]')"; $fill = mysql_query($add,$db->link) or die(mysql_error()); $id = $id + 4; }while($id < $num_items); $it = 0; do { $input = $items[$it]; $input = trim($input); $input = str_replace(" ","_",$input); //echo $input; $join = "alter table Element_$title add $input varchar(50)"; $write = mysql_query($join,$db->link) or die(mysql_error()); $it = $it + 4; }while($it < $num_items); Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/ Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 First of all, the syntax is: ALTER TABLE table_name ADD COLUMN col_name col_type; And are you sure that the table you're working with exists? Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615629 Share on other sites More sharing options...
shotos Posted August 13, 2008 Author Share Posted August 13, 2008 yes, the table does exists. it displays on the mysql server. i modify the alter command. Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615631 Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 Did fixing the ALTER syntax help any? Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615634 Share on other sites More sharing options...
shotos Posted August 13, 2008 Author Share Posted August 13, 2008 no, it didn't. just modified the error message to this: 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 'Tita ADD COLUMN car varchar(50)' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615637 Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 Do me a favor and change: $write = mysql_query($join,$db->link) or die(mysql_error()); To: $write = mysql_query($join,$db->link) or die(mysql_error() . "<br />Query was: $join"); Tell me the output. Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615639 Share on other sites More sharing options...
shotos Posted August 13, 2008 Author Share Posted August 13, 2008 this is the erroe message: 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 'Tita ADD COLUMN temperature varchar(50)' at line 1Query was: ALTER TABLE Element_ Tita ADD COLUMN temperature varchar(50) Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615646 Share on other sites More sharing options...
shotos Posted August 13, 2008 Author Share Posted August 13, 2008 maybe this may help... i used a similar statement on another script so all i had to do was just copy this part of the code and paste it there. changing only the table name........ i just tried that code and it works perfectly. just dunno why this doesn`t. Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615656 Share on other sites More sharing options...
wildteen88 Posted August 13, 2008 Share Posted August 13, 2008 You have a space between Element_ and Tita, which is causing the error. $join = 'ALTER TABLE Element_'.trim($title).' ADD COLUMN ' . $input . ' VARCHAR(50)'; Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615658 Share on other sites More sharing options...
shotos Posted August 13, 2008 Author Share Posted August 13, 2008 worked perfectly. am so grateful Quote Link to comment https://forums.phpfreaks.com/topic/119500-solved-mysql-error/#findComment-615666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.