views:

16

answers:

1

Hi guys, I really need help with this one...have spent 3 hours trying to figure it out...

Basically, I have 3 tables necessary for this function to work (the query and PHP)... Authors, Books and Users.

An author can have many books, and a user can have many books - that's it.

When the admin user selects to update a book, they are presented with a form, displaying the current data within the fields, very straight forward... However there is one tricky part, the admin user can change the author for a book (incase they make a mistake) and also change the user for which the book is associated with.

When I select to update the single book information I am not getting any values what so ever for author_id or user_id. Meaning that when the user updates the book info, the associations with the user and author is being scrapped altogether (when before there was an association)... I cannot see why this is happening because I can clearly see the IDs for the users and authors for my option values (this is because they are in select dropdowns) within the source code. Here is what my sql to retrieve the user ID is:

SELECT user_id, name FROM users

and then i have my select options which brings up all the users in the system:

<label>This book belongs to:</label> <select name="name" id="name">
<option value="<?php echo $row['user_id']?>" SELECTED><?php echo $row['name']?> - Current</option>

         <?php   
              while($row = mysql_fetch_array($result))
        { ?>                        <option value="<?php echo $row['user_id']; if (isset($_POST['user_id']));?>"><?php echo $row['name']?></option>
<?php } ?>

In the presented HTML form, I can select the users (by name) and within the source code I can see the IDs (for the value) matching against the names of the users.

Finally, in my script that performs the update, I have this:

$book_id = $_POST['book_id'];
    $bookname = $_POST['bookname'];
    $booklevel = $_POST['booklevel'];

    $author_id = $_POST['author_id'];
    $user_id = $_POST['user_id'];


    $sql = "UPDATE books SET bookname= '".$bookname."', booklevel= '".$booklevel."', author_id='".$author_id."', user_id= '".$user_id."' WHERE book_id = ".$book_id; 

The result of this query returns no value for either author_id or user_id... Obviously in this question I have given the information for the user stuff (with the HTML being displayed) but im guessing that I have the same problem with authors aswell... How can I get these ID's passed to the script so that the change can be acknowledge!! :(

+1  A: 

It looks as though the user id is being posted by the form in an element with the name "name", so you will need to read it in PHP:

$user_id = $_POST['name'];

Author ID and the other variables don't seem to be specified in the form at all, so PHP will give you empty values.

If you turn on notices in your error reporting then PHP will tell you if you try to access an undeclared array element.

SlappyTheFish
But isn't 'name' supposed to be the title of a column? I don't have a name column in my books table, instead its user_id, so if i change it to name it cant be stored anywhere can it? The author ID and other variables are called up in an identical format to users but with a different query to begin
Yvonne
In your PHP script, if you put: var_dump($_POST); you will see what variables are being sent from your form. Now, if you change the name field of your select box to "user_id" then you will see that the form passes the id in the 'user_id' field, which is what you want.
SlappyTheFish