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();
mysql pdo
add a comment |
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();
mysql pdo
I think, theSELECT DB FROM Current_DB as db
is not valid inside theINSERT
sentence. Just execute this query first, get theDB
value into a variable, and then use it withbindParam()
as you do with the other parameters.
– D. Smania
Nov 9 at 22:07
add a comment |
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();
mysql pdo
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
mysql pdo
asked Nov 9 at 21:54
Toby Dixon Smith
359
359
I think, theSELECT DB FROM Current_DB as db
is not valid inside theINSERT
sentence. Just execute this query first, get theDB
value into a variable, and then use it withbindParam()
as you do with the other parameters.
– D. Smania
Nov 9 at 22:07
add a comment |
I think, theSELECT DB FROM Current_DB as db
is not valid inside theINSERT
sentence. Just execute this query first, get theDB
value into a variable, and then use it withbindParam()
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
add a comment |
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();
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
add a comment |
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.)
add a comment |
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();
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
add a comment |
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();
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
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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.)
add a comment |
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.)
add a comment |
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.)
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.)
answered Nov 9 at 23:29
O. Jones
58.6k971106
58.6k971106
add a comment |
add a comment |
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%2f53233721%2finsert-into-table-name-with-both-variables-and-select-from%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
I think, the
SELECT DB FROM Current_DB as db
is not valid inside theINSERT
sentence. Just execute this query first, get theDB
value into a variable, and then use it withbindParam()
as you do with the other parameters.– D. Smania
Nov 9 at 22:07