carl
carl

Reputation: 41

MySQL: how to subtract an update

i need to subtract an mysql update. here is the code:

<?php
session_start();
//=============Configuring Server and Database=======
$host        =    'localhost';
$user        =    'root';
$password    =    '';
//=============Data Base Information=================
$database    =    'login';

$conn        =    mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');

//===============End Server Configuration============

//=============Starting Registration Script==========


$username    =    mysql_real_escape_string($_POST['txtusername']);

//=============To Encrypt Password===================

//============New Variable of Password is Now with an Encrypted Value========


$insert = "UPDATE `users` SET `points` = (`points`-5) WHERE `username` = '".$username."'";
$insert = "UPDATE `users` SET `lodd` = (`lodd` +1) WHERE `username` = '".$username."'";

 mysql_query($insert); 


header('location: succes.php');
?>

the +1 work perfect but it dont work to -5... how can i do so that they get -5 points?

Upvotes: 1

Views: 3912

Answers (2)

Tony
Tony

Reputation: 2754

You're overwriting the first statement with the second. Try this:

$insert = "UPDATE `users` SET `points` = (`points`-5), `lodd` = (`lodd` +1) WHERE `username` = '".$username."'";
mysql_query($insert); 

Upvotes: 0

John Woo
John Woo

Reputation: 263733

the +1 work correctly because the query with -5 will never be called as it is overwritten by the query that has +1.

you should have this code, (Although this is not the correct one)

$insert = "UPDATE `users` SET `points` = (`points`-5) WHERE `username` = '".$username."'";
mysql_query($insert);

// other codes

$insert = "UPDATE `users` SET `lodd` = (`lodd` +1) WHERE `username` = '".$username."'";
mysql_query($insert); 

follow-up question: what are the dataypes of the two columns? are they unsigned or signed?

Upvotes: 2

Related Questions