is it possible adding a column into a database table after an insert? If yes kindly give me an example code for it i don't know how to make of it
-
1Why do you want to do that? There's a chance you'll end up with a lot of columns after a long time. – hungrykoala Jun 29 '18 at 04:39
-
Sounds like you're about to make a terrible DB/table scheme. – user3783243 Jun 29 '18 at 04:56
-
_"i don't know how to"_ - Have you done any research? Any attempts at all? – M. Eriksson Jun 29 '18 at 04:56
2 Answers
yes it is possible for example :
CREATE TABLE my_table (
id integer(10),
fname varchar(50) );
and insert the values :
INSERT INTO `my_table`(`id`, `fname`) VALUES (1,'edverd');
see the value is added here
NOW Alter the Table
ALTER TABLE my_table ADD COLUMN lname varchar(50);
see the table is altered here
but the values in the added column will be null for every row present in that table.
now you can simply add the values in the added column
INSERT INTO `my_table`( `lname`) VALUES ('cullen');

- 699
- 1
- 9
- 21
To Answer Your Question: Yes you totally can.Just an FYI Insert Is basically Creating a Brand New row.(If there is something already present in the row You use UPDATE) to add Column to the database the syntax remains the same, if you are using mysql this is something your code should look like
$query="ALTER TABLE Users ADD COLUMN PASSWORD VARCHAR(100)";
$result= $mysqli->query($query);
if(!$result) echo "Failed to Add column TO The database";
BUT: 1.What you are trying to do isn't a good practice. 2.Read More about Php, You can get a course on udemy or just buy books on Php one of which is Learning PHP MYsql and Javascript By Robin Nixon, or just read more from the php manual, or mysql manual. 3.Nobody is Rude here, but lack of effort is something nobody appreciates. 4.Lastly don't get disheartened,Work Hard
-
well I wanted to differentiate between update and insert,You sure are Right, But I wanted to make it as simple as possible for him to understand. – Jun 29 '18 at 05:02
-
-
2You didn't edit `read more about this on www.w3schools.com`. Use the mysql manual or the PHP manual. W3schools has out of date content... and incorrect depending on your version of PHP/mysql. I would recommend not answering this question overall because it is asking for a horrible approach. It's the equivalent of going to a car mechanic and asking how to blow the head gasket. – user3783243 Jun 29 '18 at 05:11
-
The type of question he asked, Made he feel like he has just begun learning web Development. And I thought of suggesting the website which had the basic things explained in a very simple language, Hence w3schools.com.But I sure will add Other sources you mentioned. Thank You @user3783243 – Jun 29 '18 at 05:14
-
@user3783243 But he is still learning, I don't want to scare him off. I think We should try to help as much as possible, and also let him make mistakes and learn from them, I mean looking back Even i have made of lot of mistakes, Everybody has.I hope you get my point. – Jun 29 '18 at 05:18
-
2_"Even i have made of lot of mistakes. Everybody has"_ - True, that's why we all should help each other from making those mistakes in the first place, instead of enabling them, which is @user3783243's point. I sure wish someone had stopped me some times when I got started. A note on w3school, that site is in general very frowned upon in the development community since, as user3783243 pointed out, gives _out dated_ (and some times insecure) examples. Sending new users to that site can there for be more of a disservice than a service. – M. Eriksson Jun 29 '18 at 05:22
-
I hope this edit, makes it all right.@MagnusEriksson, If theres something still wrong Please let me know, And thanks a Bunch for all the valuable suggestions. – Jun 29 '18 at 05:48