Php mysql insert query without variables
I've always wrote 2 functions... one for insert and one for edit, for example
ADD:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
$name = isset($vars[Name]) ? $vars[Name] : "";
...
$var7= isset($vars[Var7]) ? $vars[Var7] : "";
$rs = $db->prepare($sql);
$rs->execute(array($name, ..., $var7));
$id = $db->lastInsertId();
return $id;
}
EDIT:
function edit_bank($idBank, $vars = array()) {
$sql = "UPDATE BANK SET ";
$v = array();
if ( isset($vars['Name']) ) {
$sql .= "Name = ?, ";
$v = $vars['Name'];
}
...
..
if ( isset($vars['Var7']) ) {
$sql .= "Var7= ?, ";
$v = $vars['Var7'];
}
$sql = rtrim($sql, ", ");
$sql .= " WHERE ID = ?";
$v = $idBank;
$rs = $db->prepare($sql);
$rs->execute($v);
}
Now, everytime I need to add new fields to my table, i've always to edit both function...
I'd like to reduce the problem, editing only the edit_bank function and mantain the add_bank function.
I was thinking about a solution like this:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK";
$rs = $db->prepare($sql);
$rs->execute();
$id = $db->lastInsertId();
edit_bank($id, $vars);
return $id;
}
but it seems the query: $sql = "INSERT INTO BANK"; is not valid. Any solution?
php mysql
add a comment |
I've always wrote 2 functions... one for insert and one for edit, for example
ADD:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
$name = isset($vars[Name]) ? $vars[Name] : "";
...
$var7= isset($vars[Var7]) ? $vars[Var7] : "";
$rs = $db->prepare($sql);
$rs->execute(array($name, ..., $var7));
$id = $db->lastInsertId();
return $id;
}
EDIT:
function edit_bank($idBank, $vars = array()) {
$sql = "UPDATE BANK SET ";
$v = array();
if ( isset($vars['Name']) ) {
$sql .= "Name = ?, ";
$v = $vars['Name'];
}
...
..
if ( isset($vars['Var7']) ) {
$sql .= "Var7= ?, ";
$v = $vars['Var7'];
}
$sql = rtrim($sql, ", ");
$sql .= " WHERE ID = ?";
$v = $idBank;
$rs = $db->prepare($sql);
$rs->execute($v);
}
Now, everytime I need to add new fields to my table, i've always to edit both function...
I'd like to reduce the problem, editing only the edit_bank function and mantain the add_bank function.
I was thinking about a solution like this:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK";
$rs = $db->prepare($sql);
$rs->execute();
$id = $db->lastInsertId();
edit_bank($id, $vars);
return $id;
}
but it seems the query: $sql = "INSERT INTO BANK"; is not valid. Any solution?
php mysql
1
You have to giveVALUESto the query. A correct query goes like you have above:INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)
– Svenmarim
Nov 22 '18 at 14:54
1
I wouldn't recommend your new option. Stick with the two methods or switch to an ORM and let that deal with the problems.
– Nigel Ren
Nov 22 '18 at 14:57
add a comment |
I've always wrote 2 functions... one for insert and one for edit, for example
ADD:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
$name = isset($vars[Name]) ? $vars[Name] : "";
...
$var7= isset($vars[Var7]) ? $vars[Var7] : "";
$rs = $db->prepare($sql);
$rs->execute(array($name, ..., $var7));
$id = $db->lastInsertId();
return $id;
}
EDIT:
function edit_bank($idBank, $vars = array()) {
$sql = "UPDATE BANK SET ";
$v = array();
if ( isset($vars['Name']) ) {
$sql .= "Name = ?, ";
$v = $vars['Name'];
}
...
..
if ( isset($vars['Var7']) ) {
$sql .= "Var7= ?, ";
$v = $vars['Var7'];
}
$sql = rtrim($sql, ", ");
$sql .= " WHERE ID = ?";
$v = $idBank;
$rs = $db->prepare($sql);
$rs->execute($v);
}
Now, everytime I need to add new fields to my table, i've always to edit both function...
I'd like to reduce the problem, editing only the edit_bank function and mantain the add_bank function.
I was thinking about a solution like this:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK";
$rs = $db->prepare($sql);
$rs->execute();
$id = $db->lastInsertId();
edit_bank($id, $vars);
return $id;
}
but it seems the query: $sql = "INSERT INTO BANK"; is not valid. Any solution?
php mysql
I've always wrote 2 functions... one for insert and one for edit, for example
ADD:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
$name = isset($vars[Name]) ? $vars[Name] : "";
...
$var7= isset($vars[Var7]) ? $vars[Var7] : "";
$rs = $db->prepare($sql);
$rs->execute(array($name, ..., $var7));
$id = $db->lastInsertId();
return $id;
}
EDIT:
function edit_bank($idBank, $vars = array()) {
$sql = "UPDATE BANK SET ";
$v = array();
if ( isset($vars['Name']) ) {
$sql .= "Name = ?, ";
$v = $vars['Name'];
}
...
..
if ( isset($vars['Var7']) ) {
$sql .= "Var7= ?, ";
$v = $vars['Var7'];
}
$sql = rtrim($sql, ", ");
$sql .= " WHERE ID = ?";
$v = $idBank;
$rs = $db->prepare($sql);
$rs->execute($v);
}
Now, everytime I need to add new fields to my table, i've always to edit both function...
I'd like to reduce the problem, editing only the edit_bank function and mantain the add_bank function.
I was thinking about a solution like this:
function add_bank($vars = array()) {
$sql = "INSERT INTO BANK";
$rs = $db->prepare($sql);
$rs->execute();
$id = $db->lastInsertId();
edit_bank($id, $vars);
return $id;
}
but it seems the query: $sql = "INSERT INTO BANK"; is not valid. Any solution?
php mysql
php mysql
edited Nov 22 '18 at 16:29
Yves Kipondo
1,406514
1,406514
asked Nov 22 '18 at 14:51
Giuseppe Lodi RizziniGiuseppe Lodi Rizzini
233110
233110
1
You have to giveVALUESto the query. A correct query goes like you have above:INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)
– Svenmarim
Nov 22 '18 at 14:54
1
I wouldn't recommend your new option. Stick with the two methods or switch to an ORM and let that deal with the problems.
– Nigel Ren
Nov 22 '18 at 14:57
add a comment |
1
You have to giveVALUESto the query. A correct query goes like you have above:INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)
– Svenmarim
Nov 22 '18 at 14:54
1
I wouldn't recommend your new option. Stick with the two methods or switch to an ORM and let that deal with the problems.
– Nigel Ren
Nov 22 '18 at 14:57
1
1
You have to give
VALUES to the query. A correct query goes like you have above: INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)– Svenmarim
Nov 22 '18 at 14:54
You have to give
VALUES to the query. A correct query goes like you have above: INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)– Svenmarim
Nov 22 '18 at 14:54
1
1
I wouldn't recommend your new option. Stick with the two methods or switch to an ORM and let that deal with the problems.
– Nigel Ren
Nov 22 '18 at 14:57
I wouldn't recommend your new option. Stick with the two methods or switch to an ORM and let that deal with the problems.
– Nigel Ren
Nov 22 '18 at 14:57
add a comment |
2 Answers
2
active
oldest
votes
If I'm understanding correctly you'd simply like to insert an empty row, get the new id from auto-increment column and then update it with your desired data. I've personally used this method in some situations and I don't see any problem with it.
You can insert a new row with all default values like this:
INSERT INTO BANK () VALUES();
Or alternately, if ID is an auto increment (which it appears to be) you can do:
INSERT INTO BANK SET ID = NULL;
Note for this to work all of your columns need to either have default values or you need to make sure that STRICT_TRANS_TABLES is off, otherwise you may get an error.
Now you should have a new id and an empty row to edit.
This is the best solution for my purpose!
– Giuseppe Lodi Rizzini
Nov 23 '18 at 13:27
add a comment |
You might want to look at replace into.
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT Syntax”.
This way you can create a query such as:
$sql = "INSERT INTO BANK (Id, Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
if (NEED_TO_EDIT) {
$Id = $idBank;
} else {
$Id = null;
}
$rs = $db->prepare($sql);
...
Note that this is not ansi sql standard.
This is not good: "the old row is deleted before the new row is inserted". The ID need to be the same after the edit. I can't delete the row (maybe the ID is used as FK)
– Giuseppe Lodi Rizzini
Nov 22 '18 at 15:06
For that you can useinsert on duplicate update: download.nust.na/pub6/mysql/doc/refman/5.1/en/…
– Dekel
Nov 22 '18 at 15:15
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%2f53433491%2fphp-mysql-insert-query-without-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I'm understanding correctly you'd simply like to insert an empty row, get the new id from auto-increment column and then update it with your desired data. I've personally used this method in some situations and I don't see any problem with it.
You can insert a new row with all default values like this:
INSERT INTO BANK () VALUES();
Or alternately, if ID is an auto increment (which it appears to be) you can do:
INSERT INTO BANK SET ID = NULL;
Note for this to work all of your columns need to either have default values or you need to make sure that STRICT_TRANS_TABLES is off, otherwise you may get an error.
Now you should have a new id and an empty row to edit.
This is the best solution for my purpose!
– Giuseppe Lodi Rizzini
Nov 23 '18 at 13:27
add a comment |
If I'm understanding correctly you'd simply like to insert an empty row, get the new id from auto-increment column and then update it with your desired data. I've personally used this method in some situations and I don't see any problem with it.
You can insert a new row with all default values like this:
INSERT INTO BANK () VALUES();
Or alternately, if ID is an auto increment (which it appears to be) you can do:
INSERT INTO BANK SET ID = NULL;
Note for this to work all of your columns need to either have default values or you need to make sure that STRICT_TRANS_TABLES is off, otherwise you may get an error.
Now you should have a new id and an empty row to edit.
This is the best solution for my purpose!
– Giuseppe Lodi Rizzini
Nov 23 '18 at 13:27
add a comment |
If I'm understanding correctly you'd simply like to insert an empty row, get the new id from auto-increment column and then update it with your desired data. I've personally used this method in some situations and I don't see any problem with it.
You can insert a new row with all default values like this:
INSERT INTO BANK () VALUES();
Or alternately, if ID is an auto increment (which it appears to be) you can do:
INSERT INTO BANK SET ID = NULL;
Note for this to work all of your columns need to either have default values or you need to make sure that STRICT_TRANS_TABLES is off, otherwise you may get an error.
Now you should have a new id and an empty row to edit.
If I'm understanding correctly you'd simply like to insert an empty row, get the new id from auto-increment column and then update it with your desired data. I've personally used this method in some situations and I don't see any problem with it.
You can insert a new row with all default values like this:
INSERT INTO BANK () VALUES();
Or alternately, if ID is an auto increment (which it appears to be) you can do:
INSERT INTO BANK SET ID = NULL;
Note for this to work all of your columns need to either have default values or you need to make sure that STRICT_TRANS_TABLES is off, otherwise you may get an error.
Now you should have a new id and an empty row to edit.
answered Nov 22 '18 at 16:41
billynoahbillynoah
10.9k64366
10.9k64366
This is the best solution for my purpose!
– Giuseppe Lodi Rizzini
Nov 23 '18 at 13:27
add a comment |
This is the best solution for my purpose!
– Giuseppe Lodi Rizzini
Nov 23 '18 at 13:27
This is the best solution for my purpose!
– Giuseppe Lodi Rizzini
Nov 23 '18 at 13:27
This is the best solution for my purpose!
– Giuseppe Lodi Rizzini
Nov 23 '18 at 13:27
add a comment |
You might want to look at replace into.
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT Syntax”.
This way you can create a query such as:
$sql = "INSERT INTO BANK (Id, Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
if (NEED_TO_EDIT) {
$Id = $idBank;
} else {
$Id = null;
}
$rs = $db->prepare($sql);
...
Note that this is not ansi sql standard.
This is not good: "the old row is deleted before the new row is inserted". The ID need to be the same after the edit. I can't delete the row (maybe the ID is used as FK)
– Giuseppe Lodi Rizzini
Nov 22 '18 at 15:06
For that you can useinsert on duplicate update: download.nust.na/pub6/mysql/doc/refman/5.1/en/…
– Dekel
Nov 22 '18 at 15:15
add a comment |
You might want to look at replace into.
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT Syntax”.
This way you can create a query such as:
$sql = "INSERT INTO BANK (Id, Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
if (NEED_TO_EDIT) {
$Id = $idBank;
} else {
$Id = null;
}
$rs = $db->prepare($sql);
...
Note that this is not ansi sql standard.
This is not good: "the old row is deleted before the new row is inserted". The ID need to be the same after the edit. I can't delete the row (maybe the ID is used as FK)
– Giuseppe Lodi Rizzini
Nov 22 '18 at 15:06
For that you can useinsert on duplicate update: download.nust.na/pub6/mysql/doc/refman/5.1/en/…
– Dekel
Nov 22 '18 at 15:15
add a comment |
You might want to look at replace into.
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT Syntax”.
This way you can create a query such as:
$sql = "INSERT INTO BANK (Id, Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
if (NEED_TO_EDIT) {
$Id = $idBank;
} else {
$Id = null;
}
$rs = $db->prepare($sql);
...
Note that this is not ansi sql standard.
You might want to look at replace into.
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT Syntax”.
This way you can create a query such as:
$sql = "INSERT INTO BANK (Id, Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)";
if (NEED_TO_EDIT) {
$Id = $idBank;
} else {
$Id = null;
}
$rs = $db->prepare($sql);
...
Note that this is not ansi sql standard.
answered Nov 22 '18 at 14:58
DekelDekel
43k54667
43k54667
This is not good: "the old row is deleted before the new row is inserted". The ID need to be the same after the edit. I can't delete the row (maybe the ID is used as FK)
– Giuseppe Lodi Rizzini
Nov 22 '18 at 15:06
For that you can useinsert on duplicate update: download.nust.na/pub6/mysql/doc/refman/5.1/en/…
– Dekel
Nov 22 '18 at 15:15
add a comment |
This is not good: "the old row is deleted before the new row is inserted". The ID need to be the same after the edit. I can't delete the row (maybe the ID is used as FK)
– Giuseppe Lodi Rizzini
Nov 22 '18 at 15:06
For that you can useinsert on duplicate update: download.nust.na/pub6/mysql/doc/refman/5.1/en/…
– Dekel
Nov 22 '18 at 15:15
This is not good: "the old row is deleted before the new row is inserted". The ID need to be the same after the edit. I can't delete the row (maybe the ID is used as FK)
– Giuseppe Lodi Rizzini
Nov 22 '18 at 15:06
This is not good: "the old row is deleted before the new row is inserted". The ID need to be the same after the edit. I can't delete the row (maybe the ID is used as FK)
– Giuseppe Lodi Rizzini
Nov 22 '18 at 15:06
For that you can use
insert on duplicate update: download.nust.na/pub6/mysql/doc/refman/5.1/en/…– Dekel
Nov 22 '18 at 15:15
For that you can use
insert on duplicate update: download.nust.na/pub6/mysql/doc/refman/5.1/en/…– Dekel
Nov 22 '18 at 15:15
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%2f53433491%2fphp-mysql-insert-query-without-variables%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
1
You have to give
VALUESto the query. A correct query goes like you have above:INSERT INTO BANK (Name, Var1, Var2, ...., Var7) VALUES (?, ?, ?, ..., ?)– Svenmarim
Nov 22 '18 at 14:54
1
I wouldn't recommend your new option. Stick with the two methods or switch to an ORM and let that deal with the problems.
– Nigel Ren
Nov 22 '18 at 14:57