views:

71

answers:

4

Hello,

I have this weird problem, I know how to select and insert, BUT I don't know how to UPDATE, well at least I thought I did. And all of the code from tutorials brings back errors. Here's the error I'm getting:


+1  A: 

Two problems:

1 - You are escaping the single quotes where you should not (and your double quotes are out of order).

2 - The query may be prone to SQL injection.

Try:

$tsql = mysql_query("UPDATE advertisements SET Clicks=Clicks+1, ClicksToday=ClicksToday+1 WHERE AccountNumber='".$aN."'") or die(error());
RedFilter
Thank you, that didn't bring any errors, but it didn't update the database! I tried it twice.
lucifer
wtf, Why doesn't it update the database? I have a different UPDATE statement that actually works BUT it won't work with this one, I think it may have something to do with the $aN variable as that is the only difference
lucifer
@lucifer - Have you defined the `error()` function somewhere, so you can see if there's any MySQL errors occurring by calling `mysql_error()`?
chigley
Yep, sure have. The problem has been solved now, only a couple of minutes ago. Thanks for your input :)
lucifer
+1  A: 
$tsql = mysql_query("UPDATE advertisements SET Clicks=Clicks+1, ClicksToday=ClicksToday+1 WHERE AccountNumber='".$aN."')" or die(error());
gajendra.bang
Thank you GAHB, That brought an error "Unexpected ';'. So I had to move the ) one spot to the right so it was after the final ". There are now NO errors, BUT it still **does not** update the database. I'm so confused right now
lucifer
+1  A: 

Your mysql_error ENCAPSED_STRING means that your single and double quotes are wrong. Once you've solved the problem of your quotes as RedFilter mentions, then consider your data.

Some of the problems I have encountered and eventually solved in regards to UPDATE not updating (often times I never saw any errors):

  • Check your table structure for column names being misspelled
  • Check your table structure for data input acceptance. If your table is set to receive INT(6) and your entering text (such as: $aN = "UserName"), it won't go. Additionally, check to see if you have enough space in that table cell. A column set to varchar(25) won't accept an input of 32 characters.
  • If your table structures are fine, check your data. Echo your user inputs to see that you are 1) getting input at all, 2) the input matches what your table cells will accept, and 3) that the data is sanitized for storage

I don't know if any of these suggestions will work for you, but I used to have equal frustration when I couldn't figure out what what (not) going on. These are some of the solutions that worked so that I could eventually UPDATE my tables.

Bryan
thank you so much for sharing your experiences about this. I have tried all of the above, one thing though, I tried to print/echo $aN onto the page, and it never gets displayed, so maybe it's always empty! Shouldn't be... I have updated my question to include **full** source code, please please help
lucifer
+1  A: 

The SELECT query you are running is collecting four columns, none of which are the account number.

$tsql = "SELECT WebsiteName, WebsiteDescription, WebsiteHeadline, DestinationURL FROM advertisements LIMIT 0,5"; 

What happens if you change it to add the AccountNumber field?

$tsql = "SELECT AccountNumber, WebsiteName, WebsiteDescription, WebsiteHeadline, DestinationURL FROM advertisements LIMIT 0,5"; 

At the moment, because you are not fetching the AccountNumber field, when you assign your variables, you don't have a $row['AccountNumber'].

    $aN = $row["AccountNumber"];
    $du = $row["DestinationURL"];
    $wn = $row["WebsiteName"];
    $am = $row["WebsiteDescription"];
    $ad = $row["WebsiteHeadline"];

I think this is why your UPDATE query is not updating correctly.

Aether
I'm so embarrassed right now lol, I think I need to go bang some sense into my head :P Thank you so much Aether!
lucifer