Adding books to database and their ids

Designing Solutions Through Programming. This course is also called introduction to programming.
Forum rules
Please be careful about sharing personal information. Never use any identifiable information.
Post Reply
User avatar
Merry
New User
Posts: 2
Joined: 23 Aug 2018, 11:50

Adding books to database and their ids

Post by Merry » 04 Apr 2019, 21:29

:) Hello!

I'm trying to allow the user to add a new book to the database, and this part is working. However, I also want to add the id of this book into a value from another table, in addition to other already existed ids of books, with commas separating each one of them. But the page isn't working, can you please check what is wrong?

Here is my code for add_book_process.php:

Code: Select all

<?php 
include('database_inc.php');
include('access_control.php');
session_start();

$owner = $access_control['email'];
$author = $_POST['author'];
$title = $_POST['title'];
$genre = $_POST['genre'];
$date_finished = $_POST['date_finished'];

$result = mysqli_query($connect,"INSERT INTO `book` 
(`author`, `genre`, `title`, `date_finished`) 
VALUES ('$author', '$genre', '$title', '$date_finished');");

$result2 = mysqli_query($connect,"SELECT * FROM book WHERE title LIKE '$title' AND author LIKE '$author';");
while ($row = mysqli_fetch_array($result2)){
    $added_book_id = $result2['id'];
};

$result3 = mysqli_query($connect, "SELECT * FROM book_shelf WHERE owner LIKE '$owner';");
while ($row = mysqli_fetch_array($result3)) {
        $current_book = $row['book_i_want_to_read'];

    $final_added_booklist = $current_book . "," . $added_book_id;
    $result4 = mysqli_query($connect, "UPDATE `book_shelf` SET book_i_want_to_read = replace(book_i_want_to_read, '$current_book', '$final_added_booklist');");
        };

echo '<pre>';
print_r(get_defined_vars()); 
echo '</pre>';

//header("location:index.php");
?>
Thank you!
0 x

Tags:

Post Reply