Using UPDATE and SET to add or subtract a value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pq79
Forum Newbie
Posts: 1
Joined: Thu Mar 10, 2011 4:34 am

Using UPDATE and SET to add or subtract a value

Post by pq79 »

Hello,

First time posting here so it is nice to meet you all.

I have been teaching my self php for a few weeks now and I have hit a brick wall :(

Using the following code I have been able to add or subtract values in a column within my SQL table:

Code: Select all

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot SELECT DB");

//Add query
$sql = mysql_query("UPDATE UserList SET Rewards = (Rewards + 10) WHERE Username LIKE '%$add%'");
But for the life me I can not figure out how to use a List/Menu with values of 10, 20, 30 & 40 to be the amount added rather than having a default amount in the query.

I tried:

Code: Select all

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot SELECT DB");

//Add query
$sql = mysql_query("UPDATE UserList SET Rewards = (Rewards + '%$addvalue%') WHERE Username LIKE '%$add%'");
Where 'addvaule' was:

Code: Select all

 
<form action="add_points.php" method="post" class="style13">
<input name="add" type="text" id="add" />
</label><label>
<select name="addvalue" id="addvalue">
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</form>
But it does not add the value :(

Any help anyone can provide would be really appreciated.

Many Thanks

PQ
Post Reply