1

I'm trying to call a row update from php to an mysql database. It fails out but when i try to call an insert new row formated just the same it works.

$result = mysql_query("INSERT INTO auth (username, password, studycode, description, server) VALUES ('$username', '$password', '$studycode', '$description', '$server')");

but this code fails

$result = mysql_query("UPDATE auth SET username='$username', password='$password', studycode='$studycode', description='$description', server='$server' WHERE index='$id' LIMIT 1;");

index is the first column and its the key/id for the table.

Edit: Ok so i just went into mysql admin and tried the exact command my code would have sent to track the error.

UPDATE auth SET username='username', password='password', studycode='ab9102y', description='test change', server='server2' WHERE index='5' LIMIT 1;

gives me the error

#1064 - 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' LIMIT 1' at line 1 
2
  • "Fails out" with what error? Are you sure there's a record with the ID you're passing in? Commented May 3, 2009 at 18:16
  • What error are you getting? Change the code to this: $result = mysql_query(...) or die("Error: ".mysql_error()); Commented May 3, 2009 at 18:25

5 Answers 5

7

Possibly a reserved keyword issue with index. Try:

$result = mysql_query("UPDATE auth SET username='$username', password='$password', studycode='$studycode', description='$description', server='$server' WHERE `index` ='$id'");
Sign up to request clarification or add additional context in comments.

2 Comments

You should really sanitize your input here to prevent injection, either with prepared statements or escaping the values.
It's easiest if you always put `s around all the fields as standard practice to be honest. There's a ton of reserved words and some are really good field names (desc is quite common).
1

I believe it's because the word index is a reserved word in MySQL. There for you need change your where clause to:

WHERE `index` = '$id'

Notice the ticks around index.

Comments

1

To avoid these kind of problems I usually name my tables like this: (for ex. a users table)

> CREATE TABLE usr_user (
>          usr_id INT,
>          usr_name VARCHAR(100),
>          usr_email VARCHAR(100) );

On a side note: Learn about prepared statements; I would be very scared of using the SQL code you have there.

Comments

-1

You have "UPDATE atuh" instead of "UPDATE auth". Spelling error on here or in your code?

EDIT: Try removing the quotes around your index = '$id' and also you want to remove the semi-colon from your queries in PHP.

1 Comment

just that part that I copied.
-2

try this out

$result = mysql_query("UPDATE auth SET username='$username', password='$password', studycode='$studycode', description='$description', server='$server' WHERE index='$id'");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.