Nettai manoj
Nettai manoj

Reputation: 133

updating mysql database with php variable

I want to update my mysql database, using php using variable method but it is not updating. I don't know what the problem is. This is my code:

$result = mysql_query("SELECT * FROM total") or die(mysql_error());  
$i=$row['number'];
$n=$i+1;
$result = mysql_query("UPDATE total SET number = " . $n . " WHERE number = " . $i . "") or die(mysql_error());  

How can I update my mysql database using php?

Upvotes: 0

Views: 90

Answers (4)

Bhavik Shah
Bhavik Shah

Reputation: 5183

The syntax fo your query is wrong it should be

UPDATE `total` SET number = number + 1;

you have done

UPDATE `total` S number = number + 1;

refer this mysql doc

Upvotes: 2

Robert
Robert

Reputation: 20286

It can be just with SQL without need of select. When it is not required don't use php. What can be done in mysql should be done in mysql. It's faster.

UPDATE `total` SET number = number + 1;

Moreover, you should read the red box on mysql_* documentation. These functions are depracated and will be removed in future. Consider using MYSQLI or PDO

Upvotes: 3

San
San

Reputation: 624

your query syntax is wrong, try this,

$result = mysql_query("UPDATE total SET number = '" . $n . "' WHERE number = '" . $i . "'");

Upvotes: 2

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You can increment the column_value like this column_name = column_name + 1 without using SELECT.

UPDATE total SET number = number + 1

Upvotes: 3

Related Questions