0

I think i'm doing something wrong here, I'm very new to PHP and only using it to interface my database with my client software through a WWW call, I have a Insert script, which works, but as for my Update script im stumped... here are the queries I tried:

the newest one:

$query = "UPDATE accounts SET moonscore= ' " . $moonscore . " ', sunscore = ' " . $sunscore . " ' WHERE name = ' " . $name . "';";

and I also tried, which I figured was wrong after awhile.

$query = "UPDATE accounts SET moonscore = $moonscore, sunscore = $sunscore WHERE name =$name;

Would really appreciate the help from all you PHP gurus.

4 Answers4

0

try,

$query = "UPDATE accounts
          SET    moonscore = '$moonscore', 
                 sunscore = '$sunscore'
          WHERE  name ='$name'";

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

you should use single quotes around the variables ,try this

    $query = "UPDATE accounts SET moonscore = '$moonscore' , sunscore = '$sunscore' WHERE name ='$name';

tips: try to use PDO or MYSQLI instead of mysql

echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

Your query is open for SQL Injections. I've added a simple function that always served me well.

function inject($value)
{
    // Stripslashes
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    // Quote if not integer
    if (!is_numeric($value)) {
        $value = "'" . mysql_real_escape_string($value) . "'";
    }
    return $value;
}

$query = "UPDATE accounts SET moonscore = ".inject($moonscore).", sunscore = ".inject($sunscore)." WHERE name =".inject($name);
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
0

Take a look at prepared statements to avoid having to think about protecting your queries against injection with some fancy functions. http://php.net/manual/en/pdo.prepared-statements.php

Here's a video that might give you more insight as a beginner: http://www.youtube.com/watch?v=_bw54BqS2UE

Alb Dum
  • 1,121
  • 3
  • 11
  • 26