Alesfatalis
Alesfatalis

Reputation: 777

Set MySQL field from php variable

I have a variable $Altgeraet it contains "OK" or "NOK".

I have an mysql field called Status_Altgeraet in the table zuordnung. So now I want to UPDATE or SET the field based on the variable.

This is what I tried:

$sql_Update = "UPDATE zuordnung SET Status_Altgeraet = ".$Altgeraet"";  
mysql_query($sql_Update);

Can you give me tips how to solve this? I'm a php beginner.

Upvotes: 0

Views: 122

Answers (3)

Asciiom
Asciiom

Reputation: 9975

You're missing a dot there and also quotes around the string you're trying to set the column value to (as Fluffeh mentioned the comments), try this:

$sql_Update = "UPDATE zuordnung SET Status_Altgeraet = '".$Altgeraet."'";  
mysql_query($sql_Update);

These days, prepared statements are probably the way to go, you should look into that.

Upvotes: 2

Vipin Jain
Vipin Jain

Reputation: 1402

Your current statement will lead you to set the same value to all rows of the table.

So better use the where clause in the query,

And one more thing is that the string values should be sent in query wrapped in single quotes.

See this

$sql_Update = "UPDATE zuordnung SET Status_Altgeraet = '{$Altgeraet}' WHERE id={$someid}";

HERE $someid is the variable which contains the unique identifier for the row to be updated.

or you can use combinations of 'AND' and 'OR' for more conditions to select the rows you wish to update.

EDIT

and id is integer type so do not needs to be wrapped in the single quotes. but you can always wrap integer in single quotes also.

Upvotes: 0

Fluffeh
Fluffeh

Reputation: 33512

You missed quotes around the string input and a . concat at the end of your line:

$sql_Update = "UPDATE zuordnung SET Status_Altgeraet = '".$Altgeraet."'";  

Upvotes: 3

Related Questions