ferlicia Posted July 25, 2011 Share Posted July 25, 2011 i have a existing table and let the user add another column in the table, how does the user input go into the database? i can create the column but do not know how to insert the user input. Quote Link to comment Share on other sites More sharing options...
irkevin Posted July 25, 2011 Share Posted July 25, 2011 "i have a existing table and let the user add another column in the table" You mean you let the user alter you database tables? i can create the column but do not know how to insert the user input. Maybe and INSERT INSERT INTO tableName(your_fields) VALUES(your_values) Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted July 26, 2011 Share Posted July 26, 2011 no she wants to create a TABLE column using MYSQL try this: ALTER TABLE table_name ADD (column_name varchar(number here)); Quote Link to comment Share on other sites More sharing options...
ferlicia Posted July 26, 2011 Author Share Posted July 26, 2011 lets say i have a table inside the database, name database. i have id, name, price. after that i decide to let the user add a column name quantity, i can create the column already. however, i tried to insert the user input data into the column, it cannot be done because there is an error message. the message is, Column count doesn't match value count at row 1. this is my code, <?php include 'dbFunctions.php'; $coulmnname = $_POST['columnname']; $addname = $_POST['addname']; $query = "INSERT INTO table ($columnname) VALUES ('".$addname."')"; $status = mysqli_query($link,$query) or die(mysqli_error($link)); if($status){ $message = " addded sucessfully"; } else{ $message = "Unable to add"; } Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 typo in $coulmnname Quote Link to comment Share on other sites More sharing options...
ferlicia Posted July 26, 2011 Author Share Posted July 26, 2011 i have corrected the typo, but it is still the same. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 is you database table actually called 'table' ? you need to correct that. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted July 26, 2011 Share Posted July 26, 2011 look i am confused are you trying to add a column or a row? if you want a column you need to look into the ALTER TABLE syntax. otherwise if your adding a row into MYSQL use INSERT INTO Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 My guess is ferlicia wants to add a row of data, for 2 reasons: 1. Because ferlicia said "i can create the column but do not know how to insert the user input. " 2. because letting users freely add columns do tables is such a bad idea that I'd rather think option 1 is the correct one. So what you need ferlicia, (as said before by irkevin) is something like: mysql_query("INSERT INTO `tableName` (`field1`,`field2`,`filed3`) values ('$value1','$value2','$value3')"); and make sure you have the correct table name: $field = trim($_POST['columnname']); $addname = trim($_POST['addname']); $query = "INSERT INTO `CHANGE_THIS_TO_YOUR_TABLE_NAME` ($field) VALUES ('$addname')"; $status = mysqli_query($link,$query) or die(mysqli_error($link)); Quote Link to comment 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.