Insert data into a column if no data in column
How do I insert data into a column if that column has no values? This piece of code will not insert any values, unless I take out the if statement. Of course that isn't what I want, since that means each time the code is run, more data will be inserted into the column 'datagroup'.
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
# fetch rows from data
while($row = $stmt->fetch()){
if (empty($row['datagroup'])){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
}
P.S dg_config.php contains the variable $db which is used for MySQL connection. Also, I have no values in the column so UPDATE won't work.
Desired output:
php mysql sql-insert
add a comment |
How do I insert data into a column if that column has no values? This piece of code will not insert any values, unless I take out the if statement. Of course that isn't what I want, since that means each time the code is run, more data will be inserted into the column 'datagroup'.
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
# fetch rows from data
while($row = $stmt->fetch()){
if (empty($row['datagroup'])){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
}
P.S dg_config.php contains the variable $db which is used for MySQL connection. Also, I have no values in the column so UPDATE won't work.
Desired output:
php mysql sql-insert
did you try if ($row['datagroup']==''){
– Patrick Simard
Nov 21 '18 at 14:11
You have more values than columns.
– Funk Forty Niner
Nov 21 '18 at 14:13
@FunkFortyNiner think they are multiple rows.
– Nigel Ren
Nov 21 '18 at 14:14
@NigelRen I think they want to concat them into 1 column. Hard to say really what they want to do here. Question's unclear IMHO.
– Funk Forty Niner
Nov 21 '18 at 14:14
Not sure what you want. A table is made out of columns and then has multiple rows. How you mean: one column only once? Put a unique key on the column? Do you realise that you're inserting multiple rows into the table? And check your naming. Very confusing to have a table name and column with the same name.
– Jørgen
Nov 21 '18 at 14:14
add a comment |
How do I insert data into a column if that column has no values? This piece of code will not insert any values, unless I take out the if statement. Of course that isn't what I want, since that means each time the code is run, more data will be inserted into the column 'datagroup'.
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
# fetch rows from data
while($row = $stmt->fetch()){
if (empty($row['datagroup'])){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
}
P.S dg_config.php contains the variable $db which is used for MySQL connection. Also, I have no values in the column so UPDATE won't work.
Desired output:
php mysql sql-insert
How do I insert data into a column if that column has no values? This piece of code will not insert any values, unless I take out the if statement. Of course that isn't what I want, since that means each time the code is run, more data will be inserted into the column 'datagroup'.
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
# fetch rows from data
while($row = $stmt->fetch()){
if (empty($row['datagroup'])){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
}
P.S dg_config.php contains the variable $db which is used for MySQL connection. Also, I have no values in the column so UPDATE won't work.
Desired output:
php mysql sql-insert
php mysql sql-insert
edited Nov 21 '18 at 14:24
wei123
asked Nov 21 '18 at 14:08
wei123wei123
456
456
did you try if ($row['datagroup']==''){
– Patrick Simard
Nov 21 '18 at 14:11
You have more values than columns.
– Funk Forty Niner
Nov 21 '18 at 14:13
@FunkFortyNiner think they are multiple rows.
– Nigel Ren
Nov 21 '18 at 14:14
@NigelRen I think they want to concat them into 1 column. Hard to say really what they want to do here. Question's unclear IMHO.
– Funk Forty Niner
Nov 21 '18 at 14:14
Not sure what you want. A table is made out of columns and then has multiple rows. How you mean: one column only once? Put a unique key on the column? Do you realise that you're inserting multiple rows into the table? And check your naming. Very confusing to have a table name and column with the same name.
– Jørgen
Nov 21 '18 at 14:14
add a comment |
did you try if ($row['datagroup']==''){
– Patrick Simard
Nov 21 '18 at 14:11
You have more values than columns.
– Funk Forty Niner
Nov 21 '18 at 14:13
@FunkFortyNiner think they are multiple rows.
– Nigel Ren
Nov 21 '18 at 14:14
@NigelRen I think they want to concat them into 1 column. Hard to say really what they want to do here. Question's unclear IMHO.
– Funk Forty Niner
Nov 21 '18 at 14:14
Not sure what you want. A table is made out of columns and then has multiple rows. How you mean: one column only once? Put a unique key on the column? Do you realise that you're inserting multiple rows into the table? And check your naming. Very confusing to have a table name and column with the same name.
– Jørgen
Nov 21 '18 at 14:14
did you try if ($row['datagroup']==''){
– Patrick Simard
Nov 21 '18 at 14:11
did you try if ($row['datagroup']==''){
– Patrick Simard
Nov 21 '18 at 14:11
You have more values than columns.
– Funk Forty Niner
Nov 21 '18 at 14:13
You have more values than columns.
– Funk Forty Niner
Nov 21 '18 at 14:13
@FunkFortyNiner think they are multiple rows.
– Nigel Ren
Nov 21 '18 at 14:14
@FunkFortyNiner think they are multiple rows.
– Nigel Ren
Nov 21 '18 at 14:14
@NigelRen I think they want to concat them into 1 column. Hard to say really what they want to do here. Question's unclear IMHO.
– Funk Forty Niner
Nov 21 '18 at 14:14
@NigelRen I think they want to concat them into 1 column. Hard to say really what they want to do here. Question's unclear IMHO.
– Funk Forty Niner
Nov 21 '18 at 14:14
Not sure what you want. A table is made out of columns and then has multiple rows. How you mean: one column only once? Put a unique key on the column? Do you realise that you're inserting multiple rows into the table? And check your naming. Very confusing to have a table name and column with the same name.
– Jørgen
Nov 21 '18 at 14:14
Not sure what you want. A table is made out of columns and then has multiple rows. How you mean: one column only once? Put a unique key on the column? Do you realise that you're inserting multiple rows into the table? And check your naming. Very confusing to have a table name and column with the same name.
– Jørgen
Nov 21 '18 at 14:14
add a comment |
1 Answer
1
active
oldest
votes
If you want to check if there are no records first and only insert the values then, a quick way from where you are is to say if the fetch()
fails to retrieve a row then do the insert...
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
if ( !$stmt->fetch() ){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
// re fetch the data
$stmt->execute();
This removes the while()
loop altogether.
the data does get inserted into the database but when I try to display it on the table on my web browser, the record 'Data Definitions' is missing. I inserted this codewhile($row = $stmt->fetch()){ ?> <tr> <td><?php echo $row['datagroup'] ?></td> <td><?php echo $row['dataowner']?></td> <td><?php echo $row['linkedpg']?></td> </tr> <?php } ?>
after your code.
– wei123
Nov 21 '18 at 14:52
Can you check on the database if the value is there
– Nigel Ren
Nov 21 '18 at 14:53
the values are there but I can't retrieve the first row: link
– wei123
Nov 21 '18 at 14:55
I've altered the answer slightly, this should re start the fetch.
– Nigel Ren
Nov 21 '18 at 15:02
Ah thanks! I got it working now.
– wei123
Nov 21 '18 at 15:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413896%2finsert-data-into-a-column-if-no-data-in-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to check if there are no records first and only insert the values then, a quick way from where you are is to say if the fetch()
fails to retrieve a row then do the insert...
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
if ( !$stmt->fetch() ){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
// re fetch the data
$stmt->execute();
This removes the while()
loop altogether.
the data does get inserted into the database but when I try to display it on the table on my web browser, the record 'Data Definitions' is missing. I inserted this codewhile($row = $stmt->fetch()){ ?> <tr> <td><?php echo $row['datagroup'] ?></td> <td><?php echo $row['dataowner']?></td> <td><?php echo $row['linkedpg']?></td> </tr> <?php } ?>
after your code.
– wei123
Nov 21 '18 at 14:52
Can you check on the database if the value is there
– Nigel Ren
Nov 21 '18 at 14:53
the values are there but I can't retrieve the first row: link
– wei123
Nov 21 '18 at 14:55
I've altered the answer slightly, this should re start the fetch.
– Nigel Ren
Nov 21 '18 at 15:02
Ah thanks! I got it working now.
– wei123
Nov 21 '18 at 15:06
add a comment |
If you want to check if there are no records first and only insert the values then, a quick way from where you are is to say if the fetch()
fails to retrieve a row then do the insert...
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
if ( !$stmt->fetch() ){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
// re fetch the data
$stmt->execute();
This removes the while()
loop altogether.
the data does get inserted into the database but when I try to display it on the table on my web browser, the record 'Data Definitions' is missing. I inserted this codewhile($row = $stmt->fetch()){ ?> <tr> <td><?php echo $row['datagroup'] ?></td> <td><?php echo $row['dataowner']?></td> <td><?php echo $row['linkedpg']?></td> </tr> <?php } ?>
after your code.
– wei123
Nov 21 '18 at 14:52
Can you check on the database if the value is there
– Nigel Ren
Nov 21 '18 at 14:53
the values are there but I can't retrieve the first row: link
– wei123
Nov 21 '18 at 14:55
I've altered the answer slightly, this should re start the fetch.
– Nigel Ren
Nov 21 '18 at 15:02
Ah thanks! I got it working now.
– wei123
Nov 21 '18 at 15:06
add a comment |
If you want to check if there are no records first and only insert the values then, a quick way from where you are is to say if the fetch()
fails to retrieve a row then do the insert...
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
if ( !$stmt->fetch() ){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
// re fetch the data
$stmt->execute();
This removes the while()
loop altogether.
If you want to check if there are no records first and only insert the values then, a quick way from where you are is to say if the fetch()
fails to retrieve a row then do the insert...
include('dg_config.php');
# select column datagroup from table data
$stmt = $db -> prepare("SELECT datagroup FROM data");
$stmt->execute();
if ( !$stmt->fetch() ){
$insertdg = "INSERT INTO data (datagroup)
VALUES ('Data Definitions'),('Policies'),('Systems and Process Documents'),('Purchase Orders'),('Invoices')";
$db ->exec($insertdg);
}
// re fetch the data
$stmt->execute();
This removes the while()
loop altogether.
edited Nov 21 '18 at 15:01
answered Nov 21 '18 at 14:27
Nigel RenNigel Ren
25.9k61832
25.9k61832
the data does get inserted into the database but when I try to display it on the table on my web browser, the record 'Data Definitions' is missing. I inserted this codewhile($row = $stmt->fetch()){ ?> <tr> <td><?php echo $row['datagroup'] ?></td> <td><?php echo $row['dataowner']?></td> <td><?php echo $row['linkedpg']?></td> </tr> <?php } ?>
after your code.
– wei123
Nov 21 '18 at 14:52
Can you check on the database if the value is there
– Nigel Ren
Nov 21 '18 at 14:53
the values are there but I can't retrieve the first row: link
– wei123
Nov 21 '18 at 14:55
I've altered the answer slightly, this should re start the fetch.
– Nigel Ren
Nov 21 '18 at 15:02
Ah thanks! I got it working now.
– wei123
Nov 21 '18 at 15:06
add a comment |
the data does get inserted into the database but when I try to display it on the table on my web browser, the record 'Data Definitions' is missing. I inserted this codewhile($row = $stmt->fetch()){ ?> <tr> <td><?php echo $row['datagroup'] ?></td> <td><?php echo $row['dataowner']?></td> <td><?php echo $row['linkedpg']?></td> </tr> <?php } ?>
after your code.
– wei123
Nov 21 '18 at 14:52
Can you check on the database if the value is there
– Nigel Ren
Nov 21 '18 at 14:53
the values are there but I can't retrieve the first row: link
– wei123
Nov 21 '18 at 14:55
I've altered the answer slightly, this should re start the fetch.
– Nigel Ren
Nov 21 '18 at 15:02
Ah thanks! I got it working now.
– wei123
Nov 21 '18 at 15:06
the data does get inserted into the database but when I try to display it on the table on my web browser, the record 'Data Definitions' is missing. I inserted this code
while($row = $stmt->fetch()){ ?> <tr> <td><?php echo $row['datagroup'] ?></td> <td><?php echo $row['dataowner']?></td> <td><?php echo $row['linkedpg']?></td> </tr> <?php } ?>
after your code.– wei123
Nov 21 '18 at 14:52
the data does get inserted into the database but when I try to display it on the table on my web browser, the record 'Data Definitions' is missing. I inserted this code
while($row = $stmt->fetch()){ ?> <tr> <td><?php echo $row['datagroup'] ?></td> <td><?php echo $row['dataowner']?></td> <td><?php echo $row['linkedpg']?></td> </tr> <?php } ?>
after your code.– wei123
Nov 21 '18 at 14:52
Can you check on the database if the value is there
– Nigel Ren
Nov 21 '18 at 14:53
Can you check on the database if the value is there
– Nigel Ren
Nov 21 '18 at 14:53
the values are there but I can't retrieve the first row: link
– wei123
Nov 21 '18 at 14:55
the values are there but I can't retrieve the first row: link
– wei123
Nov 21 '18 at 14:55
I've altered the answer slightly, this should re start the fetch.
– Nigel Ren
Nov 21 '18 at 15:02
I've altered the answer slightly, this should re start the fetch.
– Nigel Ren
Nov 21 '18 at 15:02
Ah thanks! I got it working now.
– wei123
Nov 21 '18 at 15:06
Ah thanks! I got it working now.
– wei123
Nov 21 '18 at 15:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413896%2finsert-data-into-a-column-if-no-data-in-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
did you try if ($row['datagroup']==''){
– Patrick Simard
Nov 21 '18 at 14:11
You have more values than columns.
– Funk Forty Niner
Nov 21 '18 at 14:13
@FunkFortyNiner think they are multiple rows.
– Nigel Ren
Nov 21 '18 at 14:14
@NigelRen I think they want to concat them into 1 column. Hard to say really what they want to do here. Question's unclear IMHO.
– Funk Forty Niner
Nov 21 '18 at 14:14
Not sure what you want. A table is made out of columns and then has multiple rows. How you mean: one column only once? Put a unique key on the column? Do you realise that you're inserting multiple rows into the table? And check your naming. Very confusing to have a table name and column with the same name.
– Jørgen
Nov 21 '18 at 14:14