INSERT INTO table name with both variables and SELECT FROM









up vote
0
down vote

favorite












I'm trying to insert an item into a table using both posted variables and something from another table. I'm not quite sure where I am going wrong because nothing is being added to the table. I'm super confused. Here is my code:



$stmt = $conn->prepare("INSERT INTO Student_Choices (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
VALUES (:username,:t1choice,:t2choice,:t3choice, db.DB)
SELECT DB FROM Current_DB as db
");
$stmt->bindParam(':username', $_SESSION['username']);
$stmt->bindParam(':t1choice', $_POST["term1sport"]);
$stmt->bindParam(':t2choice', $_POST["term2sport"]);
$stmt->bindParam(':t3choice', $_POST["term3sport"]);
$stmt->execute();









share|improve this question





















  • I think, the SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, get the DB value into a variable, and then use it with bindParam() as you do with the other parameters.
    – D. Smania
    Nov 9 at 22:07














up vote
0
down vote

favorite












I'm trying to insert an item into a table using both posted variables and something from another table. I'm not quite sure where I am going wrong because nothing is being added to the table. I'm super confused. Here is my code:



$stmt = $conn->prepare("INSERT INTO Student_Choices (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
VALUES (:username,:t1choice,:t2choice,:t3choice, db.DB)
SELECT DB FROM Current_DB as db
");
$stmt->bindParam(':username', $_SESSION['username']);
$stmt->bindParam(':t1choice', $_POST["term1sport"]);
$stmt->bindParam(':t2choice', $_POST["term2sport"]);
$stmt->bindParam(':t3choice', $_POST["term3sport"]);
$stmt->execute();









share|improve this question





















  • I think, the SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, get the DB value into a variable, and then use it with bindParam() as you do with the other parameters.
    – D. Smania
    Nov 9 at 22:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to insert an item into a table using both posted variables and something from another table. I'm not quite sure where I am going wrong because nothing is being added to the table. I'm super confused. Here is my code:



$stmt = $conn->prepare("INSERT INTO Student_Choices (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
VALUES (:username,:t1choice,:t2choice,:t3choice, db.DB)
SELECT DB FROM Current_DB as db
");
$stmt->bindParam(':username', $_SESSION['username']);
$stmt->bindParam(':t1choice', $_POST["term1sport"]);
$stmt->bindParam(':t2choice', $_POST["term2sport"]);
$stmt->bindParam(':t3choice', $_POST["term3sport"]);
$stmt->execute();









share|improve this question













I'm trying to insert an item into a table using both posted variables and something from another table. I'm not quite sure where I am going wrong because nothing is being added to the table. I'm super confused. Here is my code:



$stmt = $conn->prepare("INSERT INTO Student_Choices (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
VALUES (:username,:t1choice,:t2choice,:t3choice, db.DB)
SELECT DB FROM Current_DB as db
");
$stmt->bindParam(':username', $_SESSION['username']);
$stmt->bindParam(':t1choice', $_POST["term1sport"]);
$stmt->bindParam(':t2choice', $_POST["term2sport"]);
$stmt->bindParam(':t3choice', $_POST["term3sport"]);
$stmt->execute();






mysql pdo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 21:54









Toby Dixon Smith

359




359











  • I think, the SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, get the DB value into a variable, and then use it with bindParam() as you do with the other parameters.
    – D. Smania
    Nov 9 at 22:07
















  • I think, the SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, get the DB value into a variable, and then use it with bindParam() as you do with the other parameters.
    – D. Smania
    Nov 9 at 22:07















I think, the SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, get the DB value into a variable, and then use it with bindParam() as you do with the other parameters.
– D. Smania
Nov 9 at 22:07




I think, the SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, get the DB value into a variable, and then use it with bindParam() as you do with the other parameters.
– D. Smania
Nov 9 at 22:07












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










The SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, then get the DB value into a variable, and finally use it with bindParam() as you do with the other parameters: Something like this:



/* Get the database name. */

$stmt = $conn->prepare("SELECT DB FROM DB_Year");
$stmt->execute();
$res = $stmt->fetchAll();
$db = $res[0]['DB'];

/* Execute the insert statement. */

$stmt = $conn->prepare(
"INSERT INTO Student_Choices (Username, T1_Choice, T2_Choice, T3_Choice, Current_DB)
VALUES (:username, :t1choice, :t2choice, :t3choice, :db)"
);

$stmt->bindParam(':username', $_SESSION['username']);
$stmt->bindParam(':t1choice', $_POST["term1sport"]);
$stmt->bindParam(':t2choice', $_POST["term2sport"]);
$stmt->bindParam(':t3choice', $_POST["term3sport"]);
$stmt->bindParam(':db', $db);
$stmt->execute();





share|improve this answer






















  • Your solution almost worked. In the second statement, Current_DB should have been DB_Year. Also couldn't get the $res[0]-> DB; working so I wrote 'while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) $db = $row['DB'];' @D.Smania
    – Toby Dixon Smith
    Nov 9 at 22:42











  • @TobyDixonSmith I have updated with your feedback, check if now it is ok! Also, take care of check the answer as correct or vote it up is this helps you. Thanks!
    – D. Smania
    Nov 9 at 22:47


















up vote
0
down vote













To design this INSERT query, start by using SELECT to create the result set you want to insert.



 SELECT :username AS Username,
:t1choice AS t1choice,
:t2choice AS t2choice,
:t3choice AS t3Choice,
DB
FROM Current_DB


Then use that result set as the data source for your insert.



 INSERT INTO Student_Choices 
(Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
SELECT :username AS Username,
:t1choice AS t1choice,
:t2choice AS t2choice,
:t3choice AS t3Choice,
DB
FROM Current_DB


Notice how the SELECT operation replaces the VALUES() clause.



(Careful, unless you put an appropriate WHERE clause on the SELECT, you may get lots of rows inserted, one for each row in Current_DB.)






share|improve this answer




















    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',
    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%2f53233721%2finsert-into-table-name-with-both-variables-and-select-from%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








    up vote
    1
    down vote



    accepted










    The SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, then get the DB value into a variable, and finally use it with bindParam() as you do with the other parameters: Something like this:



    /* Get the database name. */

    $stmt = $conn->prepare("SELECT DB FROM DB_Year");
    $stmt->execute();
    $res = $stmt->fetchAll();
    $db = $res[0]['DB'];

    /* Execute the insert statement. */

    $stmt = $conn->prepare(
    "INSERT INTO Student_Choices (Username, T1_Choice, T2_Choice, T3_Choice, Current_DB)
    VALUES (:username, :t1choice, :t2choice, :t3choice, :db)"
    );

    $stmt->bindParam(':username', $_SESSION['username']);
    $stmt->bindParam(':t1choice', $_POST["term1sport"]);
    $stmt->bindParam(':t2choice', $_POST["term2sport"]);
    $stmt->bindParam(':t3choice', $_POST["term3sport"]);
    $stmt->bindParam(':db', $db);
    $stmt->execute();





    share|improve this answer






















    • Your solution almost worked. In the second statement, Current_DB should have been DB_Year. Also couldn't get the $res[0]-> DB; working so I wrote 'while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) $db = $row['DB'];' @D.Smania
      – Toby Dixon Smith
      Nov 9 at 22:42











    • @TobyDixonSmith I have updated with your feedback, check if now it is ok! Also, take care of check the answer as correct or vote it up is this helps you. Thanks!
      – D. Smania
      Nov 9 at 22:47















    up vote
    1
    down vote



    accepted










    The SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, then get the DB value into a variable, and finally use it with bindParam() as you do with the other parameters: Something like this:



    /* Get the database name. */

    $stmt = $conn->prepare("SELECT DB FROM DB_Year");
    $stmt->execute();
    $res = $stmt->fetchAll();
    $db = $res[0]['DB'];

    /* Execute the insert statement. */

    $stmt = $conn->prepare(
    "INSERT INTO Student_Choices (Username, T1_Choice, T2_Choice, T3_Choice, Current_DB)
    VALUES (:username, :t1choice, :t2choice, :t3choice, :db)"
    );

    $stmt->bindParam(':username', $_SESSION['username']);
    $stmt->bindParam(':t1choice', $_POST["term1sport"]);
    $stmt->bindParam(':t2choice', $_POST["term2sport"]);
    $stmt->bindParam(':t3choice', $_POST["term3sport"]);
    $stmt->bindParam(':db', $db);
    $stmt->execute();





    share|improve this answer






















    • Your solution almost worked. In the second statement, Current_DB should have been DB_Year. Also couldn't get the $res[0]-> DB; working so I wrote 'while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) $db = $row['DB'];' @D.Smania
      – Toby Dixon Smith
      Nov 9 at 22:42











    • @TobyDixonSmith I have updated with your feedback, check if now it is ok! Also, take care of check the answer as correct or vote it up is this helps you. Thanks!
      – D. Smania
      Nov 9 at 22:47













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    The SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, then get the DB value into a variable, and finally use it with bindParam() as you do with the other parameters: Something like this:



    /* Get the database name. */

    $stmt = $conn->prepare("SELECT DB FROM DB_Year");
    $stmt->execute();
    $res = $stmt->fetchAll();
    $db = $res[0]['DB'];

    /* Execute the insert statement. */

    $stmt = $conn->prepare(
    "INSERT INTO Student_Choices (Username, T1_Choice, T2_Choice, T3_Choice, Current_DB)
    VALUES (:username, :t1choice, :t2choice, :t3choice, :db)"
    );

    $stmt->bindParam(':username', $_SESSION['username']);
    $stmt->bindParam(':t1choice', $_POST["term1sport"]);
    $stmt->bindParam(':t2choice', $_POST["term2sport"]);
    $stmt->bindParam(':t3choice', $_POST["term3sport"]);
    $stmt->bindParam(':db', $db);
    $stmt->execute();





    share|improve this answer














    The SELECT DB FROM Current_DB as db is not valid inside the INSERT sentence. Just execute this query first, then get the DB value into a variable, and finally use it with bindParam() as you do with the other parameters: Something like this:



    /* Get the database name. */

    $stmt = $conn->prepare("SELECT DB FROM DB_Year");
    $stmt->execute();
    $res = $stmt->fetchAll();
    $db = $res[0]['DB'];

    /* Execute the insert statement. */

    $stmt = $conn->prepare(
    "INSERT INTO Student_Choices (Username, T1_Choice, T2_Choice, T3_Choice, Current_DB)
    VALUES (:username, :t1choice, :t2choice, :t3choice, :db)"
    );

    $stmt->bindParam(':username', $_SESSION['username']);
    $stmt->bindParam(':t1choice', $_POST["term1sport"]);
    $stmt->bindParam(':t2choice', $_POST["term2sport"]);
    $stmt->bindParam(':t3choice', $_POST["term3sport"]);
    $stmt->bindParam(':db', $db);
    $stmt->execute();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 22:47

























    answered Nov 9 at 22:21









    D. Smania

    2,6541321




    2,6541321











    • Your solution almost worked. In the second statement, Current_DB should have been DB_Year. Also couldn't get the $res[0]-> DB; working so I wrote 'while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) $db = $row['DB'];' @D.Smania
      – Toby Dixon Smith
      Nov 9 at 22:42











    • @TobyDixonSmith I have updated with your feedback, check if now it is ok! Also, take care of check the answer as correct or vote it up is this helps you. Thanks!
      – D. Smania
      Nov 9 at 22:47

















    • Your solution almost worked. In the second statement, Current_DB should have been DB_Year. Also couldn't get the $res[0]-> DB; working so I wrote 'while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) $db = $row['DB'];' @D.Smania
      – Toby Dixon Smith
      Nov 9 at 22:42











    • @TobyDixonSmith I have updated with your feedback, check if now it is ok! Also, take care of check the answer as correct or vote it up is this helps you. Thanks!
      – D. Smania
      Nov 9 at 22:47
















    Your solution almost worked. In the second statement, Current_DB should have been DB_Year. Also couldn't get the $res[0]-> DB; working so I wrote 'while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) $db = $row['DB'];' @D.Smania
    – Toby Dixon Smith
    Nov 9 at 22:42





    Your solution almost worked. In the second statement, Current_DB should have been DB_Year. Also couldn't get the $res[0]-> DB; working so I wrote 'while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) $db = $row['DB'];' @D.Smania
    – Toby Dixon Smith
    Nov 9 at 22:42













    @TobyDixonSmith I have updated with your feedback, check if now it is ok! Also, take care of check the answer as correct or vote it up is this helps you. Thanks!
    – D. Smania
    Nov 9 at 22:47





    @TobyDixonSmith I have updated with your feedback, check if now it is ok! Also, take care of check the answer as correct or vote it up is this helps you. Thanks!
    – D. Smania
    Nov 9 at 22:47













    up vote
    0
    down vote













    To design this INSERT query, start by using SELECT to create the result set you want to insert.



     SELECT :username AS Username,
    :t1choice AS t1choice,
    :t2choice AS t2choice,
    :t3choice AS t3Choice,
    DB
    FROM Current_DB


    Then use that result set as the data source for your insert.



     INSERT INTO Student_Choices 
    (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
    SELECT :username AS Username,
    :t1choice AS t1choice,
    :t2choice AS t2choice,
    :t3choice AS t3Choice,
    DB
    FROM Current_DB


    Notice how the SELECT operation replaces the VALUES() clause.



    (Careful, unless you put an appropriate WHERE clause on the SELECT, you may get lots of rows inserted, one for each row in Current_DB.)






    share|improve this answer
























      up vote
      0
      down vote













      To design this INSERT query, start by using SELECT to create the result set you want to insert.



       SELECT :username AS Username,
      :t1choice AS t1choice,
      :t2choice AS t2choice,
      :t3choice AS t3Choice,
      DB
      FROM Current_DB


      Then use that result set as the data source for your insert.



       INSERT INTO Student_Choices 
      (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
      SELECT :username AS Username,
      :t1choice AS t1choice,
      :t2choice AS t2choice,
      :t3choice AS t3Choice,
      DB
      FROM Current_DB


      Notice how the SELECT operation replaces the VALUES() clause.



      (Careful, unless you put an appropriate WHERE clause on the SELECT, you may get lots of rows inserted, one for each row in Current_DB.)






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        To design this INSERT query, start by using SELECT to create the result set you want to insert.



         SELECT :username AS Username,
        :t1choice AS t1choice,
        :t2choice AS t2choice,
        :t3choice AS t3Choice,
        DB
        FROM Current_DB


        Then use that result set as the data source for your insert.



         INSERT INTO Student_Choices 
        (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
        SELECT :username AS Username,
        :t1choice AS t1choice,
        :t2choice AS t2choice,
        :t3choice AS t3Choice,
        DB
        FROM Current_DB


        Notice how the SELECT operation replaces the VALUES() clause.



        (Careful, unless you put an appropriate WHERE clause on the SELECT, you may get lots of rows inserted, one for each row in Current_DB.)






        share|improve this answer












        To design this INSERT query, start by using SELECT to create the result set you want to insert.



         SELECT :username AS Username,
        :t1choice AS t1choice,
        :t2choice AS t2choice,
        :t3choice AS t3Choice,
        DB
        FROM Current_DB


        Then use that result set as the data source for your insert.



         INSERT INTO Student_Choices 
        (Username,T1_Choice,T2_Choice,T3_Choice,Current_DB)
        SELECT :username AS Username,
        :t1choice AS t1choice,
        :t2choice AS t2choice,
        :t3choice AS t3Choice,
        DB
        FROM Current_DB


        Notice how the SELECT operation replaces the VALUES() clause.



        (Careful, unless you put an appropriate WHERE clause on the SELECT, you may get lots of rows inserted, one for each row in Current_DB.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 23:29









        O. Jones

        58.6k971106




        58.6k971106



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233721%2finsert-into-table-name-with-both-variables-and-select-from%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo