tags:

views:

60

answers:

1

I'm trying to a single value in my DB...When I run it through the console, it works correctly (as I'm replacing the variables with numbers and text).. However, My query is not returning a value for book ID when I insert the PHP variable for it.. It's because the book_id is unpopulated...

$query = "UPDATE books "
       . "SET readstatus='".$readstatus."' "
       . "WHERE book_id=".$book_id;
echo $query

The echoed query states: UPDATE books SET readstatus='half' WHERE book_id=0

The book ID is stored in the URI as bookstatusupdate.php?book_id=

Just cannot figure this one out!:(

+3  A: 

It would help to know the error. Firstly, echo out the query:

$query = "UPDATE books "
       . "SET readstatus='".$readstatus."' "
       . "WHERE book_id=".$book_id;
echo $query;

I would guess that $book_id is unpopulated, so the query fails. What you should really be doing to make it secure is casting integers with (int) and wrapping strings in mysqli_real_escape_string().

$query = "UPDATE books "
        ."SET readstatus='". mysqli_real_escape_string( $readstatus )."' "
        ."WHERE book_id=". (int) $book_id;

If you're trying to get data from the URL, do it like so:

$book_id = (int) $_GET['book_id'];

$query = "UPDATE books "
        ."SET readstatus='". mysqli_real_escape_string( $readstatus )."' "
        ."WHERE book_id=". (int) $book_id;

echo $query;
Andy
Ok I did this and its giving me a book ID of '0' so its obviously not being picked up anywhere, but it should be because the URI contains the ID of the book ID that is being updated in the form. Any ideas?
Jess
Is there anyway that I can use the vlaue in the URI and set it as the book ID?
Jess
Sounds like you're used to having `register_globals()` on - this should never be the case, it's dangerous! Disable it in your php.ini if it's not already. To get data from the URL, use $_GET['book_id'] - you can see all the parameters that have been passed using print_r( $_GET ); - HTH
Andy
I already have this as you have specified in the edited question, and still no luck. i dont understand as the ID is being successfully display in the URI on the update page!!
Jess
What's the URL/URI and what do you get from print_r( $_GET )? You can edit your answer to display these parameters, it'll have better formatting.
Andy