How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask TheDoctor Your Own Question
TheDoctor, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1757
Experience:  M.S. in Internet Information Systems
Type Your Homework Question Here...
TheDoctor is online now
A new question is answered every 9 seconds

PHP/MySQL: Need debugging help on 2 pages. Adding books to

This answer was rated:

PHP/MySQL: Need debugging help on 2 pages. Adding books to tables in a database and editing book data of tables in a database. Will be using INSERT and UPDATE into 3 tables. I have four other pages similar but I believe if I can get two to work, I can use that information in the other four.


Specifics: Using a php form and code to add information to a MySQL database and to edit information in a MySQL database.

Hi my name isXXXXX for using JustAnswer. Can you provide the code you are working with? Thanks.
Customer: replied 4 years ago.

Yes, I can provide code and database dump. Should I attach it here?

Paste it at and paste the URL of the upload here that way all of the code can be seen. Thanks.

This appears to be for a course or homework assignment.

Can you please confirm.

Thank you
Customer: replied 4 years ago.


Hello again,

Think you for confirming. We will need to move the question into the homework category. I can assist you with this.

Zip up your code and database dump. You can either attach it to this thread or upload it to

Once uploaded, you will be given a link on the right. Click on the 'Copy Link' option and then paste it here.

Once I have your files, I will be able to assist you.

Thank you
Customer: replied 4 years ago.

There is already someone else helping me - Eljon. Is he still helping? I just sent him the code a few minutes ago.

You didn't actually upload the code. You would have had to paste the URL into this thread, which you did not do.

The other professional does not work in the Homework category. However, I am a Zend Certified Engineer (PHP Expert) with many years of experience and can certainly provide you with assistance.
I'm still here. Waiting for you to send the code.
Customer: replied 4 years ago.

Did you get it that time?

Customer: replied 4 years ago.

Hello again,

Thank you for providing the code. I will contact you once I am done debugging everything.

Thank you
Customer: replied 4 years ago.

okay, thank you


you can also find the Web site this code is being used on at

Hello again,

Can you please also send me the included files? It is very difficult to debug a file when it has a number of includes that I do not have. I have no way of knowing any of them are behaving badly.

Your addBook.php file includes:


For instance, without these files I will always get "You must be logged in..." I can comment all of that code out, but then I am not really testing what you have.
Customer: replied 4 years ago.

new file with includes folder is at

Thank you so much. I will contact you soon.
Hello again!

So, I had to make some changes in order to get this to work correctly:

You can download the files here:

First, please notice that there is a file called alters.sql

This file contains statements that you need to run on your database.

Here was the issue. An author or a publisher can have more than one book. So putting a bookID in the authors or the publisher table is not good and will lead to many issues. Additionally, the way you were attemptint to perform inserts on those tables will never work.

Once you have run all of that SQL, you will be ready to use the changed files.

I changed the queries in each file to make certain that both the add and edit functionality works.

You had a minor issue where you were calling mysql_error when you are using mysqli. I corrected those for you.

The code should be pretty self-explanitory. Take note of the ON DUPLICATE KEY UPDATE statements. This states that if the author or the publisher already exists, we just want to get the ID of the existing author or publisher. That way, if you enter the name of an author or a publisher that is already in the database, the book will be associated to the existing records rather than create new ones.

You can immediately see the edit page work by tyin the URL in as "editBooks.php?id=1"

However, if you have a page somewhere that lists books, it probably isn't going to work after the database changes because of the change to the way that books are associated to authors and publishers. To fix this, go into editBooks.php and look at lines 130 - 141. This is the select statement to get the book information now that the tables have been corrected. Actually, the way that you had the tables was a violation of Normal Form, so if I hadn't fixed them, your instructor probably would have said something.

Let me know if you have any questions. I'm here to help.

Please remember to rate my answer. Thank you so much!
Hello again, Kim!

I wanted to let you know that if you have any issues at all, please feel free to bring them up. For instance, if you have other files (such as a file that lists all books or anything like that), and you cannot get it to work with the new database structure, please do not hesitate to send it to me. I will gladly update that file for you. Just send whatever you need help with.

Thank you again!
Customer: replied 4 years ago.

Did you get my last reply? I don't see it here. Anyway, what I said was I had two other joining tables like you created but I couldn't get my query to work so I thought it might be easier with only the three, so I reworked them the bookID to them.


And you were, right, totally messed up my viewBooks.php file and I have uploaded at

No, I didn't receive your last reply. But now I know.

I will get your viewBooks.php fixed and send it to you soon.

Thank you
Customer: replied 4 years ago.

I did try the editBooks.php code and it didn't work. It may have to do with the pagination and sort code that is on the viewBooks.php

Hello again!

Please download the file here:

That should fix that page for you. Please do not hesitate to ask me any other questions.

If you require assistance in the future, please feel free to request me directly by starting your quesitons with **For TheDoctor**. It would be my pleasure to assist you again.

Please remember to rate my answer. Thank you so much!

You need to spend $3 to view this post. Add Funds to your account and buy credits.
TheDoctor, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1757
Experience: M.S. in Internet Information Systems
TheDoctor and 3 other Homework Specialists are ready to help you
Customer: replied 4 years ago.

Thanks, XXXXX XXXXX great.


I see what you're talking about. I had attempted to add a book earlier, and it looks like it added the book information but did not add the author or publisher information to the other two tables. I'll delete that book manually from the database.

Excellent! If you need anything else, just let me know!

Have a great night.
Customer: replied 4 years ago.

You too. I actually have music and movies to do the same way, and now I need to go back and re-add the tables I deleted for those. Luckily I have an old copy of those somewhere.

Remember to add the unique key to the name on the movies or music. See how I did that in the SQL for the other two? I added UNIQUE to authName and pubName.

That way, when an insert is attempted, the unique key will actually trigger the "ON DUPLICATE KEY UPDATE" part. Without the unique key, it will not trigger that part.
Customer: replied 4 years ago.

I will. Thank you again. I've been working on these for weeks, while also working on other files on the page.

You are very welcome.