Jump to content

Using UPDATE and SET to add or subtract a value


pquinn

Recommended Posts

Hello,

 

Hope I am in the right part of the forum for this :(

 

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 within a column in my SQL table:

 


// 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:

 


// 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 the id for the value:

 

 
<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>
<input type="submit" name="submit" value=" Reward " />
</form>

 

Hope I am making sense head is going to explode lol,.

 

Any help and advice would be very much appreciated.

 

P Quinn

Link to comment
Share on other sites

Hi,

 

Thanks I have adjusted my code to be;

 

<?php $add = $_POST['add']; ?>
<?php $addvalue = $_POST['addvalue']?>
<?php
// Database details
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="UserList"; // Table name

// 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 + '%$addvaule%') WHERE Username LIKE '%$add%'");

?>

 

And the action is processed but the value does not update, any advice would be great.

 

Many thanks,

 

P Quinn

Link to comment
Share on other sites

hmmmm :(

 

The value will now update correctly but I have three columns: Rewards, Sanctions and TotalPoints

 

The values for Rewards and Sanctions will update correctly but the Total Points will not.

 

On page subtract_points.php the code I have updated to look like:

 

<?php 
$subtract = $_POST['subtract'];
$subtractvalue = $_POST['subtractvalue '];

// Database details
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="UserList"; // Table name

// 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");


//Update Total Points query
$sql = mysql_query("UPDATE UserList SET TotalPoints = (TotalPoints - $subtractvalue) WHERE Username LIKE '%$subtract%'");

?>

 

Basically as I reward or sanction I want the total points to reflect only the points removed so if I had picked 30 rewards and rewards the Rewards to be at 30 and TotalPoints to be at 30, but if i remove 40 I want the TotalPoints to show -10. 

 

It was working before I used the list menu that is working fine now for adding and subtracting. So close :( any help would be great

 

Many Thanks,

 

P Quinn

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.