Php mysql insert query without variables












1















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?










share|improve this question




















  • 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








  • 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















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?










share|improve this question




















  • 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








  • 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








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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





    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





    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





    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












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer
























  • This is the best solution for my purpose!

    – Giuseppe Lodi Rizzini
    Nov 23 '18 at 13:27



















0














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.






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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.






share|improve this answer
























  • This is the best solution for my purpose!

    – Giuseppe Lodi Rizzini
    Nov 23 '18 at 13:27
















1














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.






share|improve this answer
























  • This is the best solution for my purpose!

    – Giuseppe Lodi Rizzini
    Nov 23 '18 at 13:27














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













0














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.






share|improve this answer
























  • 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
















0














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.






share|improve this answer
























  • 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














0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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 use insert 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











  • 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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out