-5

I have this query:

$date = "january";
mysqli_query($connection->dbconn, "UPDATE monthly_visitors SET '$date' = 0");

But for some reason it does not work. The variable doesn't seem to work inside the query. Ideas why this is happening?

  • if you have more than the one row, well... that is a problem here and most likely why it is failing. Plus, `January` and `january` are two different animals, should that be the case. No idea what you want to do here though. Show us the db schema for this. You have a string and an integer. Columns don't use quotes, btw, they use backticks `\`` – Funk Forty Niner Aug 06 '16 at 00:30
  • What does error reporting say? Are you sure `january` is the name of the column, not a value? – Kevin Kopf Aug 06 '16 at 00:34
  • So what i'm trying to do is a simple "visitor counter", the database has 12 rows, which is january, february, mars, april, etc. Now the $date will be changed to display the current month with date(F), and strtolower to match my database rows. I only need it to update the the row which matches the current month, thats why I use the variable $date. – newbieDevs Aug 06 '16 at 00:35
  • actually, your db won't entirely get updated (yet) because it never made it there in the first place. Why? Because, you have a major syntax error which is caused by the initial quote injection, but you didn't check for errors to start with; that's one of the faults with this. Here, see for yourself http://php.net/manual/en/mysqli.error.php and apply that to your query. – Funk Forty Niner Aug 06 '16 at 00:40
  • possible duplicate of [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – Funk Forty Niner Aug 06 '16 at 00:51

1 Answers1

0

It seems like you're trying to set the value in a column called "january" to zero for all rows. Is that really what you want to do? If so then try to remove the quotes around "$date". It shouldn't be quoted because it's a column name.

Also, note that if $date would come from a user then this code snippet would be susceptible for SQL injections. Never use user input directly in a query. Verify it / escape it / use parameters instead.

obe
  • 7,378
  • 5
  • 31
  • 40
  • [didn't I say that? Oh yeah, I did... just in a different way.](http://stackoverflow.com/questions/38799414/php-mysqli-query-update-not-working-with-variable#comment64968946_38799414) *lol* – Funk Forty Niner Aug 06 '16 at 00:38
  • Right... sorry, I didn't notice :-/ – obe Aug 06 '16 at 00:40