MySQL, How to add a points system? (stacking variables in a row.) - php

What I am trying to do is add a points system to my script that will add a small point value to their row, I have tried the following queries to my database. (Not all at once, lol)
$query = "INSERT INTO `users` SET `points` = '$points' WHERE `username` = '$username'";
$query = "UPDATE `users` SET `points` + 5 WHERE `username` = '$username'";
$query = "UPDATE `users` SET `points` = '$points' WHERE `username` = '$username'";
I have gotten it to ADD the points, but each time I do another action that would in return add more points to my current point balance it just updates with that amount of points, instead of adding that sum of points to the already existing balance.
edit:
The variable for points that I was using is:
$points + 5; OR $points = $points + 5;

Close. Try this:
$query = "UPDATE `users` SET points = points + 5 WHERE `username` = '$username'";

Related

Updating MYSQL table row column sum by appending

I would like to update a column row inside MYSQL by adding 50 to the last current number.Means column named sum was 200 and after query, it will update to 250.
I can do this by using two query as follow:
$add1 = 50;
$sql = "SELECT sum FROM table_name WHERE id = '$id' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);
$add2 = $row['sum'] + $add1 ;
}
$sql1 = "UPDATE tabel_name SET sum = '$add2' WHERE id = '$id' ";
$result1 = $conn->query($sql1);
However, is there a better way to do this with less code or by one go?
Thanks
You can calculate values directly in UPDATE queries.
$add1 = 50;
$sql1 = "UPDATE tabel_name SET sum = sum + $add1 WHERE id = '$id';";
In case the type of your column sum is not numeric, but contians a string of the given number, you can use the MySQL CAST function to cast it to a number, calculate it and cast it back to a string again:
$sql1 = "UPDATE tabel_name SET sum = CAST((CAST(sum as INT(11)) + $add1) as VARCHAR(11)) WHERE id = '$id';";
Why dont you try By
$sql1 = "UPDATE tabel_name SET sum = sum + 'YOUR VALUE' WHERE id = '$id'
You can do it using UPDATE for this, just change your query like:
$sql = "UPDATE table_name SET sum=sum+value_you_want WHERE id = '$id' ";
Please use this sql query:
$sql1 = "UPDATE tabel_name SET sum = sum + 50 WHERE id = '$id' ";

Pass sql variable into PHP variable then back into sql statement

I want to select the highest value in a table:
$max = "SELECT MAX(pid) FROM pic";
Then pass that value into a PHP variable:
$results_max = $conn->query($max);
$highest_val = $results_max->fetch_assoc();
To then use again in a SQL insert statement:
$sql_update = "UPDATE users
SET username = '$username', pid = '$highest_val'
WHERE username = '$username'";
However i tested out the value i got from my first select statement ($highest_val) and it returns "Array". Does anyone know what I am doing wrong?
Edit:
$sql_update = "UPDATE users
SET username = '$username', pic_id = '$highest_val[pid]'
WHERE username = '$username'" ;
You need to create alias of MAX(pid);
$max = "SELECT MAX(pid) as pid FROM pic";
Now you fetch max pid using
$results_max = $conn->query($max);
$highest = $results_max->fetch_assoc();
$highest_val =$highest['pid'];// pass column name here
And your Update query would be
$sql_update = "UPDATE users
SET username = '".$username."', pid = '".$highest_val."'
WHERE username = '".$username."'";

My MySQLi dosen't update only inserts? What's wrong with my script?

$check_verified_user = mysqli_query("SELECT * from user_verified where user_mail = '$payer_email'");
$user_verified = mysqli_fetch_array(mysqli_query($conDB, "SELECT * FROM user_verified where user_mail = '$payer_email'"));
if(mysqli_num_rows($check_verified_user) > 0) {
mysqli_query($conDB, "UPDATE user_verified SET total_paid = total_paid + '$payment_amount' where user_mail = '$payer_email'");
} else {
mysqli_query($conDB, "INSERT into user_verified (user_mail,total_paid) VALUES ('$payer_email', '$payment_amount')");
}
I don't know what's wrong with my script, it checks if the row exists, then if it exists it should update, but instead it inserts another row, which i don't understand...
Give this a try.
$query = "SELECT * FROM user_verified WHERE user_mail = $payer_email";
$check_verified_user = mysqli_query($conDB, $query);
You basically wasn't giving it the database connection, so it was always coming back as not being greater than 0 rows. Personally, I always put my query into its own variable first, and this will ensure that you don't forget params for the mysqli_query() function. It also makes it easier to read, and allows you to use the query in other places if needed.
You can do this method:
$sql = "SELECT * from user_verified where user_mail = '$payer_email'";
$result = mysqli_query($conDB, $sql) or trigger_error(mysqli_error($conDB));
if (mysqli_num_rows($result)) {
$sql = "UPDATE user_verified SET total_paid = total_paid + '$payment_amount' where user_mail = '$payer_email'";
}
else {
$sql = "INSERT into user_verified (user_mail,total_paid) VALUES ('$payer_email', '$payment_amount')";
}
mysqli_free_result($result);
mysqli_query($conDB, $sql) or trigger_error(mysqli_error($conDB));
If your user_mail is a UNIQUE KEY you would be able to use the option ON DUPLICATE KEY UPDATE in the INSERT. You can do everything in just 1 query.
$sql = "INSERT INTO user_verified (user_mail,total_paid) VALUES ('$payer_email',$payment_amount) ON DUPLICATE KEY UPDATE total_paid = total_paid + $payment_amount";
mysqli_query($conDB, $sql);
Check this example of a table using UNIQUE KEY.
http://sqlfiddle.com/#!2/4919e2/1/0

SQL update row if key exists else update a default row

Here is what I am trying to do (in pseudo code)
if (dB entry exists) {
UPDATE the dB entry
}else{
UPDATE a default dB entry
}
This is not a "INSERT INTO ... ON DUPLICATE" question.
I'm hoping there is some kind of UPDATE ... ON DUPLICATE type of slick code to do this in one line.
My code creates $userName from the $_GET['U'] request. If $userName exists in the database (it's a unique key), then increment a counter in the database. Else, increment a counter for a default entry.
Here is my current code to update the counter:
$userName = $_GET['U'];
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = '" . $userName . "'";
mysqli_query($conn,$sql);
And if this particular username doesn't exist, I want this to happen:
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = 'default'";
mysqli_query($conn,$sql);
Are you looking for this,
$userName = $_GET['U'];
$query_uname_exists = "SELECT count(*) FROM stats WHERE `userName` = '" . $userName . "'";
$uname_count = mysqli_query($conn,$query_uname_exists);
if($uname_count > 0){
#update
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = '" . $userName . "'";
mysqli_query($conn,$sql);
}
else{
#update default
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = 'default'";
mysqli_query($conn,$sql);
}
Use the following query
$sql = "Update stats SET count = count + 1 where username = (case when username= ". $userName ". then username else 'default' end)";
This is the most I can shrink it for you:
$userName = mysqli_query($conn, "SELECT count(*) FROM stats WHERE `userName` = '".$_GET['U']."' LIMIT 1") == 0 ? 'default' : $_GET['U'];
mysqli_query($conn, "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = '".$userName."' LIMIT 1");

UPDATE mysql_query not updating table

Im having trouble with the UPDATE command.
Im trying to update my db but its just not happening. Ive been trying to get this to work for the last 10 days and its driving me nuts.
Here is the code:
$a = mysql_query("UPDATE `findacab` SET `lat` = ".$ads['Latitude']." , `long` = ".$ads['Longitude']."
WHERE `eeventendtime` = ".$ads['Postcode']." ");
Table:
$q = mysql_query("SELECT Postcode, Latitude, Longitude FROM postcodes");
while($ads = mysql_fetch_array($q))
{
mysql_query("UPDATE findacab SET lat = '".$ads['Latitude']."' , long = '".$ads['Longitude']."' WHERE eeventendtime = '".$ads['Postcode']."' ");
echo $ads['Latitude']." ".$ads['Longitude']." ".$ads['Postcode']."</br>";
//$query = "select count(*) from findacab where eeventendtime = '".mysql_real_escape_string($ads['Postcode'])."'";
}
Unless your complete table consists of only integer you should add quotations around your strings
$a = mysql_query("UPDATE `findacab` SET
`lat` = '".$ads['Latitude']."' ,
`long` = '".$ads['Longitude']."'
WHERE
`eeventendtime` = '".$ads['Postcode']."' ");
$query = "select count(*) from findacab where eeventendtime = '".mysql_real_escape_string($ads['Postcode'])."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row[0]
If it returns 0 then you just don't have records to update.
Another possible reason - you are trying to update table with same values as stored. In this case update will not change the data.

Categories