-1

Using this code:

$sql = mysql_query("UPDATE tablename SET auth = '$new_auth' WHERE index = '$index'");

I printed out all the variables. It's working correctly, it's just not updating the auth or sometimes with playing around the code it'll just update the first result

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Swaly
  • 89
  • 8
  • 2
    What are the variables? How do you know it's working correctly? Are you checking the return value of `mysql_query()`? Is there anything in `mysql_error()` – andrewsi Aug 12 '13 at 19:34
  • $new_auth is a randomly generated auth, $index is the index of the old auth code. no errors what so ever + + printing out the variables return the correct ones – Swaly Aug 12 '13 at 19:35
  • How are you connecting to your database, where are you setting the variables and are you getting any errors? – Tricky12 Aug 12 '13 at 19:36
  • How do you know that you successfully connected to the database? How do you know that the table exists? How do you know that the `update` statement succeeded? How do you know that rabid weasels didn't chew through your network cables while you were waiting for the result of the `update` statement? –  Aug 12 '13 at 19:36
  • 2
    You shouldn't I use mysql_* functions in PHP: http://stackoverflow.com/q/12859942/358906 – Nabil Kadimi Aug 12 '13 at 19:37
  • use PDO [forget](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) the [mysql extension](http://www.php.net/manual/en/function.mysql-connect.php). – Songo Aug 12 '13 at 22:11

5 Answers5

1

When I'm having issues with my queries I like to use or die() for trouble shooting. I would try updating your code with the following:

$sql = mysql_query("UPDATE tablename SET auth = $new_auth WHERE index = $index") or die(mysql_error());

This will kick back an error involving that query and will give us a better understanding of what the problem actually is.

Victus
  • 51
  • 3
  • But he says he has tried printing out the errors and there was nothing. – Annabel Aug 12 '13 at 19:46
  • @Annabel / Hash, I got this error after tweaking the code abit You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index = 5' at line 1 – Swaly Aug 12 '13 at 22:44
  • @Swaly, you need to cheque your table to see what the types of the `auth` and `index` columns are. My code suggestion (below) assumed `auth` was a string and `index` was an integer so I put quotes around `$new_auth`. @Hash Gambit's code (above) assumes both `auth` and `index` are integers so he didn't put quotes around any variable. – Annabel Aug 12 '13 at 22:51
  • Index is an integer with an auto increment as extra and auth is varchar(32) – Swaly Aug 12 '13 at 23:07
0

In your table does index have a string type? If not and index is numeric leaving out the ' characters around $index will do the trick.

$sql = mysql_query("UPDATE tablename SET auth = '$new_auth' WHERE index = $index");
Annabel
  • 1,394
  • 14
  • 23
0

After much research I figured out that the reason this didn't work is because index is a reserved word in MYSQL code, I fixed it by wrapping index with 2 ticks

Swaly
  • 89
  • 8
0

Please try this code with using of Acute(`) cause index is a reserve work of mysql:

$sql = mysql_query("UPDATE tablename SET auth = '$new_auth' WHERE `index` = $index");
Chinmay235
  • 3,236
  • 8
  • 62
  • 93
-2

your $sql is wrong : you need to write it like that:

$sql = mysql_query("UPDATE tablename SET auth = '".$new_auth."' WHERE index = '".$index."'");
Momo1987
  • 544
  • 6
  • 16
  • 1
    The string "u" is not a word in the English language. That aside, you're wrong. –  Aug 12 '13 at 19:37
  • 1
    This is wrong because in PHP when variables are expanded inside strings delimited by `"` characters. See http://php.net/manual/en/language.types.string.php – Annabel Aug 12 '13 at 19:42
  • how wrong pls did someone try it? check here http://php.net/manual/de/function.mysql-query.php ow you need to write a mysql_query – Momo1987 Aug 12 '13 at 19:44
  • 1
    @Momo1987: no, the problem with your answer is that you don't know how strings work in in PHP. In PHP `$a = 'hello'; $b = "$a"; echo $b;` outputs 'hello'; – Annabel Aug 12 '13 at 19:49