Forum Moderators: coopster

Message Too Old, No Replies

IDs necessary in update script not being stored (or even seen!?)

passing IDs to an update script, unpopulated!

         

scankified

10:26 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



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! :(

Matthew1980

10:43 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there scankified,

Just something to point out really:-

<option value="<?php echo $row['user_id']?>" SELECTED><?php echo $row['name']?> - Current</option>

is missing the semi-colon's ;-) Should be:-

<option value="<?php echo $row['user_id'];?>" SELECTED><?php echo $row['name'];?> - Current</option>

Are you or have you done any $_POST checking to see if the values are actually set when submit is actioned?

And from what I can see, the if() clause in this is not doing anything and the semi-colon is missing from the $row['name']; :-

<?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 } ?>

unless this is a way of checking something, it's a new one on me ;-)

Sorry not to be more constructive, but I just thought I should point the semi-colon issue out there..

Cheers,
MRb

scankified

11:33 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



hey MRb, it seems on my end that the semi-colons at the end of the echo statements only matter when there are multiple statements in 1 row; e.g. <?php echo 'Hello'; if (isset($_POST['hellothere']));?>

Matthew1980

8:14 am on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there scankified,

WRT the echo: echo "Hi there"; will output to screen: Hi there, if you leave off the ; from the end of the " you would get an error, something like, UNEXPECTED (description) WAS EXPECTING ;)} ON LINE (the corresponding line), put the error_reporting(E_ALL); on to see if this would be the case.

The semi-colon ; is the instruction terminator, basically what ever preceed's this symbol get's actioned by php when its parsed, without the ; the preceeding text would/could in theory be printed to screen but there would be at least a NOTICE thrown if you had error_reporting; turned on ;-p

Again though, I'm not sure what this does:-

if (isset($_POST['hellothere']));

If you are just needing to echo the contents of this specific key do somthing like this:-

echo (isset($_POST['hellothere']));

This just simply says, echo (only if it's set) as you can use empty or isset, as they both have the same functionality.

Cheers,
MRb