Asked  7 Months ago    Answers:  5   Viewed   28 times

I have made this code for giving out +1 point, but it doesn't work properly.

mysql_query("
    UPDATE member_profile 
    SET points= ' ".$points." ' + 1 
    WHERE user_id = '".$userid."'
");

the $points variable is the user´s points right now.. I want it to plus one to it.. so example if he had like 5 points, it should be 5+1 = 6.. but it doesnt, it just changes to 1

What have i done wrong? thank you

 Answers

81

You could also just do this:

mysql_query("
    UPDATE member_profile 
    SET points = points + 1
    WHERE user_id = '".$userid."'
");
Wednesday, March 31, 2021
 
Null
answered 7 Months ago
32

If your column name starts with a number, you have to quote it in backticks:

$query = "UPDATE users SET Points=Points+3 WHERE `1A`=$m1A";

and:

$query1 = "UPDATE rounds SET `1A` = 1";

And I would recommend using a prepared statement with bound parameters to avoid sql injection problems.

Edit: If your 1A column is not an integer column and the values are strings, you need to quote them.

$query = "UPDATE users SET Points=Points+3 WHERE `1A`='$m1A'";
                                                      ^    ^

Although that problem would be solved automatically with a prepared statement...

Wednesday, March 31, 2021
 
avon_verma
answered 7 Months ago
37

You have to execute the query.

$userId = 1; // some number
$stmt->execute(array($userId));

Or you could combine them into one using method chaining:

 $db->prepare(
     "UPDATE `ulog` SET `logged_in`=0, `signout_time`=NOW(),
     `ckey`= '', `ctime`= '' WHERE user_id=? AND logged_in=1"
 )->execute(array($userId));

Also, make sure your $userid is being set. Do a var_dump() on it and see what you get.

Saturday, May 29, 2021
 
Vlad
answered 5 Months ago
71

Are you sure, your $permalink assignment is correct? Based on your wanted output, I created a snippet for the permalink and tested the code(also corrected the mysqli_error function..)

$sql = "SELECT * FROM titles";
$query = mysqli_query($con, $sql) or die (mysqli_error($con));
while ($row = mysqli_fetch_array($query)) {
    $id = $row["id"];
    $title = $row["title"];

    // title rewrite function goes here
    //$permalink = preg_replace ..... etc
    $permalink = preg_replace('~s+~', '-', strtolower($title));

    //add new permalink rewrite to slug column
    $sql2 = "UPDATE titles SET permalink='$permalink' WHERE id='$id'";
    $query2 = mysqli_query($con, $sql2) or die (mysqli_error($con));
}

This code works fine for me. If this doesn't create the correct output, something might be wrong with your database table data

Btw. you could use the sql procedure from https://stackoverflow.com/a/8419128/1043150 and break the wohle code down to a single sql statement

UPDATE titles SET permalink = slugify(title)
Saturday, May 29, 2021
 
Hugo
answered 5 Months ago
85

You are mixing INSERT and UPDATE syntax.

$q3 = "UPDATE `post` SET `$keyresult` = '$valueresult' 
       WHERE user_id='$user_id' AND post_id='$post_id' AND post_status='active'";

I am assuming you are properly escaping $valueresult, $user_id, and $post_id before you are executing your query. If not, and these are user-supplied values, you are wide open to SQL injections. I recommend looking into prepared statements to eliminate this risk.

Saturday, May 29, 2021
 
Elxx
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :