...

View Full Version : incrementing a value in mysql



Armondo
03-25-2007, 02:50 AM
ok, so incrementing a value in mysql is easy enough...but how would i increment a value on command? like if a user does something it increments it...like i connect to the database and like...++ it. kinda like in flat files where you do $i++ or something. i can't get it to work. would i like get the number from the database and then increment it with php then re put it? i need help...
i have this code:



// not really finished...i have no idea what i am doing.
$get = MYSQL_QUERY("SELECT points from users WHERE username='$username' and email='$email'");
// matches username with email already saved in a session(?)
// how would i get a variable out of points from the database?
$points = $points++; // is this even possible?

$result= MYSQL_QUERY("INSERT INTO users (points)".
"VALUES ($points)");


heh...well i tried huh?

Fumigator
03-25-2007, 03:03 AM
One query is all you need, an update query:


UPDATE MyTable
SET points = points + 1
WHERE ......

iLLin
03-25-2007, 03:04 AM
$result= MYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$username' and email='$email'");



:)

iLLin
03-25-2007, 03:04 AM
Wow you beat me.... :p

Armondo
03-25-2007, 04:02 AM
0_0! wow thanks! i didn't know it was that simple. i am noobish when it comes to mysql, i bought some books that vaugely talk about mysql stuff such as this. yeah, i should get this up and running in a sec so i can test it. expect a lot of crap from me :).

Armondo
03-25-2007, 04:18 AM
ahhh its not working :(

my code:

<?
session_start();
header("Cache-control: private");

$assume = $_POST['assume'];
$posterEMAIL = $_POST['postemail'];
$postTXT = $_POST['posttxt'];
$posterNAME = $_POST['poster'];
$postTITLE = $_POST['posttitle'];


function filterBadWords($str){

// words to filter
$badwords=array( // removed due to badess 0_o );

// replace filtered words with
$replacements=array( "#@%*!", "$%#*$!", "@#$%*", "*#!@#", "#!~$%" );

for($i=0;$i < sizeof($badwords);$i++){
srand((double)microtime()*1000000);
$rand_key = (rand()%sizeof($replacements));
$str=eregi_replace($badwords[$i], $replacements[$rand_key], $str);
}
return $str;
}
$postTXT = filterBadWords($postTXT);
$postTITLE = filterBadWords($postTITLE);

if ($assume == "true") {

$dbcnx = mysql_connect("*", "*", "*");
mysql_select_db("flashanims_db");


$sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL',
postTXT='$postTXT', postTITLE='$postTITLE'";
$result= MYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$username' and email='$email'");
if (mysql_query($sql)) {
echo("

Your comment has been added!
");
} else {
echo("

Oh noes! there was an error adding entry: " . mysql_error() . "
");
}
}

?>
the reason i am not trying to troubleshoot is that i have two different tables in the same db...should i be able to contact both tables in one thing? i don't know. so what i am trying to do is: when they post something...it adds a point to thier profile. easy enough i assume. i just don't know how i would go about doing this.

edit: i have changed the type of "points" to decimal...does that mean it is a number? what do i need to do to make the "points" row a number that starts with zero and then as it gets incremented it goes up the scale?

iLLin
03-25-2007, 04:50 AM
Where did you define $username and $email?

Armondo
03-25-2007, 04:53 AM
in the session when you login. Does that code match the username and email together? i was wondering that...
and so...what is happening in the code? it just doesn't increment.

iLLin
03-25-2007, 05:06 AM
This is my question to you.



$sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL',
postTXT='$postTXT', postTITLE='$postTITLE'";
$result= MYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$username' and email='$email'");


Is $posterNAME and $posterEMAIL supposed to be the same as $username and $email?

IF so change your code like so.



$sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL',
postTXT='$postTXT', postTITLE='$postTITLE'";
$result= MYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$posterNAME' and email='$posterEMAIL'");

Armondo
03-25-2007, 05:07 AM
i shall try it!

edit: O M G...it works! thank you! and thanks also for helping my friend ryan. i gave you some rep :).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum