INSERT new book in t1 and include an existent author from T2 to this book
I am a student and I have just started the journey with php and mysql. If there is any mistake regarding to this post, my apologies.
I have spent hours trying to do this by myself.. feeling dumb.
Sorry I haven't found any other question that could solve this problem.
Please, how can I get this working?
I have 2 tables with auto_increment ids:
Table 1
bookid | bookTitle | authorid
1 | book1 | 1
Table 2
authorid | authorname| bookid
1 | author1 | 1
I have a form with input values for the book and input values for the author, how can I INSERT this new book and get the information of the existent author?
find below some pieces of my code, that's not the whole code, I think I am just missing the logical query to find the author name in table 2 and insert the book with it.
I would like include the existent authorid from t2 to this book.
// dbFunction file
function addBook($booktitle, $name)
$sql = "INSERT INTO book(bookTitle) VALUES (:booktitle)";
$sql = "INSERT INTO author(Name) VALUES (:name)
// controller
require ("dbFunctions.php");
if($_POST['action_type'] == 'add'){
$query = $conn->prepare( "SELECT authorname FROM author WHERE authorname = :name");
$query->bindvalue(":name", $name);
$query->execute();
if( $query->rowCount() > 1 ) {
$querySuccess = addBook($booktitle, $authorname) // calling function
if( $query->rowCount() > 1, It will insert the book and author information even if it exists, it will duplicate authors and ids.
if( $query->rowCount() < 1, it will insert the book and the author.
Thank you.
php mysql forms function insert
add a comment |
I am a student and I have just started the journey with php and mysql. If there is any mistake regarding to this post, my apologies.
I have spent hours trying to do this by myself.. feeling dumb.
Sorry I haven't found any other question that could solve this problem.
Please, how can I get this working?
I have 2 tables with auto_increment ids:
Table 1
bookid | bookTitle | authorid
1 | book1 | 1
Table 2
authorid | authorname| bookid
1 | author1 | 1
I have a form with input values for the book and input values for the author, how can I INSERT this new book and get the information of the existent author?
find below some pieces of my code, that's not the whole code, I think I am just missing the logical query to find the author name in table 2 and insert the book with it.
I would like include the existent authorid from t2 to this book.
// dbFunction file
function addBook($booktitle, $name)
$sql = "INSERT INTO book(bookTitle) VALUES (:booktitle)";
$sql = "INSERT INTO author(Name) VALUES (:name)
// controller
require ("dbFunctions.php");
if($_POST['action_type'] == 'add'){
$query = $conn->prepare( "SELECT authorname FROM author WHERE authorname = :name");
$query->bindvalue(":name", $name);
$query->execute();
if( $query->rowCount() > 1 ) {
$querySuccess = addBook($booktitle, $authorname) // calling function
if( $query->rowCount() > 1, It will insert the book and author information even if it exists, it will duplicate authors and ids.
if( $query->rowCount() < 1, it will insert the book and the author.
Thank you.
php mysql forms function insert
It's not completely clear to me what you want. Do you want people to type in an author and you match it with the database and it needs to add a book with an already existing author?
– Kerwin Sneijders
Nov 12 '18 at 1:02
Hi Kerwin, Yes, I have a form with input values: book title and author name, I want people to type the book title and also the author name in this form, then.. I want to insert this book in my database, but before insert this book in the database I want to look for an existent author on table 2 and then if there is an existent author with the same name typed in the form on table 2, I want to insert the book on table 1 using the existent author id from table 2. Thank you.
– Rennie
Nov 12 '18 at 1:13
You have syntax errors here. You also have a variable scope issue.
– Funk Forty Niner
Nov 12 '18 at 1:19
add a comment |
I am a student and I have just started the journey with php and mysql. If there is any mistake regarding to this post, my apologies.
I have spent hours trying to do this by myself.. feeling dumb.
Sorry I haven't found any other question that could solve this problem.
Please, how can I get this working?
I have 2 tables with auto_increment ids:
Table 1
bookid | bookTitle | authorid
1 | book1 | 1
Table 2
authorid | authorname| bookid
1 | author1 | 1
I have a form with input values for the book and input values for the author, how can I INSERT this new book and get the information of the existent author?
find below some pieces of my code, that's not the whole code, I think I am just missing the logical query to find the author name in table 2 and insert the book with it.
I would like include the existent authorid from t2 to this book.
// dbFunction file
function addBook($booktitle, $name)
$sql = "INSERT INTO book(bookTitle) VALUES (:booktitle)";
$sql = "INSERT INTO author(Name) VALUES (:name)
// controller
require ("dbFunctions.php");
if($_POST['action_type'] == 'add'){
$query = $conn->prepare( "SELECT authorname FROM author WHERE authorname = :name");
$query->bindvalue(":name", $name);
$query->execute();
if( $query->rowCount() > 1 ) {
$querySuccess = addBook($booktitle, $authorname) // calling function
if( $query->rowCount() > 1, It will insert the book and author information even if it exists, it will duplicate authors and ids.
if( $query->rowCount() < 1, it will insert the book and the author.
Thank you.
php mysql forms function insert
I am a student and I have just started the journey with php and mysql. If there is any mistake regarding to this post, my apologies.
I have spent hours trying to do this by myself.. feeling dumb.
Sorry I haven't found any other question that could solve this problem.
Please, how can I get this working?
I have 2 tables with auto_increment ids:
Table 1
bookid | bookTitle | authorid
1 | book1 | 1
Table 2
authorid | authorname| bookid
1 | author1 | 1
I have a form with input values for the book and input values for the author, how can I INSERT this new book and get the information of the existent author?
find below some pieces of my code, that's not the whole code, I think I am just missing the logical query to find the author name in table 2 and insert the book with it.
I would like include the existent authorid from t2 to this book.
// dbFunction file
function addBook($booktitle, $name)
$sql = "INSERT INTO book(bookTitle) VALUES (:booktitle)";
$sql = "INSERT INTO author(Name) VALUES (:name)
// controller
require ("dbFunctions.php");
if($_POST['action_type'] == 'add'){
$query = $conn->prepare( "SELECT authorname FROM author WHERE authorname = :name");
$query->bindvalue(":name", $name);
$query->execute();
if( $query->rowCount() > 1 ) {
$querySuccess = addBook($booktitle, $authorname) // calling function
if( $query->rowCount() > 1, It will insert the book and author information even if it exists, it will duplicate authors and ids.
if( $query->rowCount() < 1, it will insert the book and the author.
Thank you.
php mysql forms function insert
php mysql forms function insert
edited Nov 12 '18 at 1:20
Funk Forty Niner
80.5k1247101
80.5k1247101
asked Nov 12 '18 at 0:49
Rennie
32
32
It's not completely clear to me what you want. Do you want people to type in an author and you match it with the database and it needs to add a book with an already existing author?
– Kerwin Sneijders
Nov 12 '18 at 1:02
Hi Kerwin, Yes, I have a form with input values: book title and author name, I want people to type the book title and also the author name in this form, then.. I want to insert this book in my database, but before insert this book in the database I want to look for an existent author on table 2 and then if there is an existent author with the same name typed in the form on table 2, I want to insert the book on table 1 using the existent author id from table 2. Thank you.
– Rennie
Nov 12 '18 at 1:13
You have syntax errors here. You also have a variable scope issue.
– Funk Forty Niner
Nov 12 '18 at 1:19
add a comment |
It's not completely clear to me what you want. Do you want people to type in an author and you match it with the database and it needs to add a book with an already existing author?
– Kerwin Sneijders
Nov 12 '18 at 1:02
Hi Kerwin, Yes, I have a form with input values: book title and author name, I want people to type the book title and also the author name in this form, then.. I want to insert this book in my database, but before insert this book in the database I want to look for an existent author on table 2 and then if there is an existent author with the same name typed in the form on table 2, I want to insert the book on table 1 using the existent author id from table 2. Thank you.
– Rennie
Nov 12 '18 at 1:13
You have syntax errors here. You also have a variable scope issue.
– Funk Forty Niner
Nov 12 '18 at 1:19
It's not completely clear to me what you want. Do you want people to type in an author and you match it with the database and it needs to add a book with an already existing author?
– Kerwin Sneijders
Nov 12 '18 at 1:02
It's not completely clear to me what you want. Do you want people to type in an author and you match it with the database and it needs to add a book with an already existing author?
– Kerwin Sneijders
Nov 12 '18 at 1:02
Hi Kerwin, Yes, I have a form with input values: book title and author name, I want people to type the book title and also the author name in this form, then.. I want to insert this book in my database, but before insert this book in the database I want to look for an existent author on table 2 and then if there is an existent author with the same name typed in the form on table 2, I want to insert the book on table 1 using the existent author id from table 2. Thank you.
– Rennie
Nov 12 '18 at 1:13
Hi Kerwin, Yes, I have a form with input values: book title and author name, I want people to type the book title and also the author name in this form, then.. I want to insert this book in my database, but before insert this book in the database I want to look for an existent author on table 2 and then if there is an existent author with the same name typed in the form on table 2, I want to insert the book on table 1 using the existent author id from table 2. Thank you.
– Rennie
Nov 12 '18 at 1:13
You have syntax errors here. You also have a variable scope issue.
– Funk Forty Niner
Nov 12 '18 at 1:19
You have syntax errors here. You also have a variable scope issue.
– Funk Forty Niner
Nov 12 '18 at 1:19
add a comment |
1 Answer
1
active
oldest
votes
As you edited your post I think I understand what you mean. But before I can answer your question you need to change some things.
- Your book table and author table both link to eachother. You want only one to link to the other. Because a book always has an author you need to give the book an author id but an author has (almost always) more books. Because you can only save one value per column you should remove the
book id
from the author. If you ever find yourself in a situation where both tables can have more than one of eachother (you will), you'd need to make amany-to-many
relation. - If you want people to 'select' an author, you should not make them type the name (unless you use jquery ajax calls but I'd not recommend those if you just started with all this). I'd recommend using php to create a dropdown list with all the authors, if there are many, you can also look into datalists. (Keep in mind that datalists can be submitted without actually selecting an item from the list, so you need to check if what they chose actuallu exists)
- At last, you do not need to add another author every time you add a book. Because you can link that book to an author using it's id.
So the steps I'd recommend you do are:
- Recreate the author input field to a
HTML
select box or datalist - Check if the author selected exists (Checking if the query resulted in one row)
(SELECT * FROM authors WHERE id = :id
) Use the IDs as values in the datalist or select statement - Add a book with the selected id from the found author
1
Thanks a lot Kerwin, perfect. I was not thinking about the user select an existent author. I have just few authors, the dropdown list will work perfectly fine. I will create a select box for an existent author and make it not required, they can choose to select an existent one from the list or use the form to insert a new author. It helped a lot, not sure why people rate it -1.
– Rennie
Nov 12 '18 at 3:31
Little late at the response, but thanks. Hope it works now. About the downvotes.... I have no idea either.. I don't mind a downvote, as long as people give feedback. Anyway, all you can do is counter vote it....
– Kerwin Sneijders
Nov 15 '18 at 13:13
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%2f53254730%2finsert-new-book-in-t1-and-include-an-existent-author-from-t2-to-this-book%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
As you edited your post I think I understand what you mean. But before I can answer your question you need to change some things.
- Your book table and author table both link to eachother. You want only one to link to the other. Because a book always has an author you need to give the book an author id but an author has (almost always) more books. Because you can only save one value per column you should remove the
book id
from the author. If you ever find yourself in a situation where both tables can have more than one of eachother (you will), you'd need to make amany-to-many
relation. - If you want people to 'select' an author, you should not make them type the name (unless you use jquery ajax calls but I'd not recommend those if you just started with all this). I'd recommend using php to create a dropdown list with all the authors, if there are many, you can also look into datalists. (Keep in mind that datalists can be submitted without actually selecting an item from the list, so you need to check if what they chose actuallu exists)
- At last, you do not need to add another author every time you add a book. Because you can link that book to an author using it's id.
So the steps I'd recommend you do are:
- Recreate the author input field to a
HTML
select box or datalist - Check if the author selected exists (Checking if the query resulted in one row)
(SELECT * FROM authors WHERE id = :id
) Use the IDs as values in the datalist or select statement - Add a book with the selected id from the found author
1
Thanks a lot Kerwin, perfect. I was not thinking about the user select an existent author. I have just few authors, the dropdown list will work perfectly fine. I will create a select box for an existent author and make it not required, they can choose to select an existent one from the list or use the form to insert a new author. It helped a lot, not sure why people rate it -1.
– Rennie
Nov 12 '18 at 3:31
Little late at the response, but thanks. Hope it works now. About the downvotes.... I have no idea either.. I don't mind a downvote, as long as people give feedback. Anyway, all you can do is counter vote it....
– Kerwin Sneijders
Nov 15 '18 at 13:13
add a comment |
As you edited your post I think I understand what you mean. But before I can answer your question you need to change some things.
- Your book table and author table both link to eachother. You want only one to link to the other. Because a book always has an author you need to give the book an author id but an author has (almost always) more books. Because you can only save one value per column you should remove the
book id
from the author. If you ever find yourself in a situation where both tables can have more than one of eachother (you will), you'd need to make amany-to-many
relation. - If you want people to 'select' an author, you should not make them type the name (unless you use jquery ajax calls but I'd not recommend those if you just started with all this). I'd recommend using php to create a dropdown list with all the authors, if there are many, you can also look into datalists. (Keep in mind that datalists can be submitted without actually selecting an item from the list, so you need to check if what they chose actuallu exists)
- At last, you do not need to add another author every time you add a book. Because you can link that book to an author using it's id.
So the steps I'd recommend you do are:
- Recreate the author input field to a
HTML
select box or datalist - Check if the author selected exists (Checking if the query resulted in one row)
(SELECT * FROM authors WHERE id = :id
) Use the IDs as values in the datalist or select statement - Add a book with the selected id from the found author
1
Thanks a lot Kerwin, perfect. I was not thinking about the user select an existent author. I have just few authors, the dropdown list will work perfectly fine. I will create a select box for an existent author and make it not required, they can choose to select an existent one from the list or use the form to insert a new author. It helped a lot, not sure why people rate it -1.
– Rennie
Nov 12 '18 at 3:31
Little late at the response, but thanks. Hope it works now. About the downvotes.... I have no idea either.. I don't mind a downvote, as long as people give feedback. Anyway, all you can do is counter vote it....
– Kerwin Sneijders
Nov 15 '18 at 13:13
add a comment |
As you edited your post I think I understand what you mean. But before I can answer your question you need to change some things.
- Your book table and author table both link to eachother. You want only one to link to the other. Because a book always has an author you need to give the book an author id but an author has (almost always) more books. Because you can only save one value per column you should remove the
book id
from the author. If you ever find yourself in a situation where both tables can have more than one of eachother (you will), you'd need to make amany-to-many
relation. - If you want people to 'select' an author, you should not make them type the name (unless you use jquery ajax calls but I'd not recommend those if you just started with all this). I'd recommend using php to create a dropdown list with all the authors, if there are many, you can also look into datalists. (Keep in mind that datalists can be submitted without actually selecting an item from the list, so you need to check if what they chose actuallu exists)
- At last, you do not need to add another author every time you add a book. Because you can link that book to an author using it's id.
So the steps I'd recommend you do are:
- Recreate the author input field to a
HTML
select box or datalist - Check if the author selected exists (Checking if the query resulted in one row)
(SELECT * FROM authors WHERE id = :id
) Use the IDs as values in the datalist or select statement - Add a book with the selected id from the found author
As you edited your post I think I understand what you mean. But before I can answer your question you need to change some things.
- Your book table and author table both link to eachother. You want only one to link to the other. Because a book always has an author you need to give the book an author id but an author has (almost always) more books. Because you can only save one value per column you should remove the
book id
from the author. If you ever find yourself in a situation where both tables can have more than one of eachother (you will), you'd need to make amany-to-many
relation. - If you want people to 'select' an author, you should not make them type the name (unless you use jquery ajax calls but I'd not recommend those if you just started with all this). I'd recommend using php to create a dropdown list with all the authors, if there are many, you can also look into datalists. (Keep in mind that datalists can be submitted without actually selecting an item from the list, so you need to check if what they chose actuallu exists)
- At last, you do not need to add another author every time you add a book. Because you can link that book to an author using it's id.
So the steps I'd recommend you do are:
- Recreate the author input field to a
HTML
select box or datalist - Check if the author selected exists (Checking if the query resulted in one row)
(SELECT * FROM authors WHERE id = :id
) Use the IDs as values in the datalist or select statement - Add a book with the selected id from the found author
edited Nov 12 '18 at 14:04
Jay Blanchard
35.4k125495
35.4k125495
answered Nov 12 '18 at 1:21
Kerwin Sneijders
8913
8913
1
Thanks a lot Kerwin, perfect. I was not thinking about the user select an existent author. I have just few authors, the dropdown list will work perfectly fine. I will create a select box for an existent author and make it not required, they can choose to select an existent one from the list or use the form to insert a new author. It helped a lot, not sure why people rate it -1.
– Rennie
Nov 12 '18 at 3:31
Little late at the response, but thanks. Hope it works now. About the downvotes.... I have no idea either.. I don't mind a downvote, as long as people give feedback. Anyway, all you can do is counter vote it....
– Kerwin Sneijders
Nov 15 '18 at 13:13
add a comment |
1
Thanks a lot Kerwin, perfect. I was not thinking about the user select an existent author. I have just few authors, the dropdown list will work perfectly fine. I will create a select box for an existent author and make it not required, they can choose to select an existent one from the list or use the form to insert a new author. It helped a lot, not sure why people rate it -1.
– Rennie
Nov 12 '18 at 3:31
Little late at the response, but thanks. Hope it works now. About the downvotes.... I have no idea either.. I don't mind a downvote, as long as people give feedback. Anyway, all you can do is counter vote it....
– Kerwin Sneijders
Nov 15 '18 at 13:13
1
1
Thanks a lot Kerwin, perfect. I was not thinking about the user select an existent author. I have just few authors, the dropdown list will work perfectly fine. I will create a select box for an existent author and make it not required, they can choose to select an existent one from the list or use the form to insert a new author. It helped a lot, not sure why people rate it -1.
– Rennie
Nov 12 '18 at 3:31
Thanks a lot Kerwin, perfect. I was not thinking about the user select an existent author. I have just few authors, the dropdown list will work perfectly fine. I will create a select box for an existent author and make it not required, they can choose to select an existent one from the list or use the form to insert a new author. It helped a lot, not sure why people rate it -1.
– Rennie
Nov 12 '18 at 3:31
Little late at the response, but thanks. Hope it works now. About the downvotes.... I have no idea either.. I don't mind a downvote, as long as people give feedback. Anyway, all you can do is counter vote it....
– Kerwin Sneijders
Nov 15 '18 at 13:13
Little late at the response, but thanks. Hope it works now. About the downvotes.... I have no idea either.. I don't mind a downvote, as long as people give feedback. Anyway, all you can do is counter vote it....
– Kerwin Sneijders
Nov 15 '18 at 13:13
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53254730%2finsert-new-book-in-t1-and-include-an-existent-author-from-t2-to-this-book%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
It's not completely clear to me what you want. Do you want people to type in an author and you match it with the database and it needs to add a book with an already existing author?
– Kerwin Sneijders
Nov 12 '18 at 1:02
Hi Kerwin, Yes, I have a form with input values: book title and author name, I want people to type the book title and also the author name in this form, then.. I want to insert this book in my database, but before insert this book in the database I want to look for an existent author on table 2 and then if there is an existent author with the same name typed in the form on table 2, I want to insert the book on table 1 using the existent author id from table 2. Thank you.
– Rennie
Nov 12 '18 at 1:13
You have syntax errors here. You also have a variable scope issue.
– Funk Forty Niner
Nov 12 '18 at 1:19