blubex
blubex

Reputation: 13

Find a value in a table row, and if it is there, update it

I'm trying to find a person in my table and update their score. This is the code I have right now. For some reason it's not working. Instead of changing the person's score, it will just make a new row with the same name of the person.

$name = $_POST["strtolower(name)"];
$team = $_POST["team"];
$num = $_POST["number"];
$goals = $_POST["goals"];

if($query = mysqli_query("SELECT goals FROM goalscorers WHERE name=$name ", $db)){
  while($row = mysqli_fetch_assoc($query)){
    $origgoals = $row['goals'];
    $newgoals = (int)$origgoals + (int)$goals;
    mysqli_query($db, "UPDATE goalscorers SET goals=$newgoals WHERE name=$name ");
    echo "<h1>Thank you for submitting your details! <br /> <a href=\"goalscorers.php\">Add another</a></h1>";
  }
  mysqli_free_result($query);
}
else {
    $query = "INSERT INTO goalscorers (name, team, num, goals) VALUES ('$name','$team','$num','$goals') ";
    $result = mysqli_query($query, $db);
    if (mysqli_error()) { print "Database ERROR: " . mysql_error(); } 

    echo "<h1>Thank you for submitting your details! <br /> <a href=\"goalscorers.php\">Add another</a></h1>";
}

I'm very new to both PHP and MySQL so it's probably a basic mistake.

Also, I already am connected to the database.

Upvotes: 1

Views: 85

Answers (1)

peterm
peterm

Reputation: 92815

Your immediate problem is that you don't have quotes around string values in your sql queries. Change

"SELECT goals FROM goalscorers WHERE name=$name "

to

"SELECT goals FROM goalscorers WHERE name = '$name'"
                                            ^     ^

and

"UPDATE goalscorers SET goals=$newgoals WHERE name=$name "

to

"UPDATE goalscorers SET goals=$newgoals WHERE name = '$name'"
                                                     ^     ^

On a side note: learn and use prepared statements. Your code is vulnerable to sql injections.


UPDATE1: You can drastically simplify your code with INSERT ... ON DUPLICATE KEY UPDATE. In order for it to work properly you have to have a UNIQUE (PRIMARY KEY) index on name column. Your insert statement then should look like

INSERT INTO goalscorers (`name`, `team`, `num`, `goals`)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE goals = goals + VALUES(goals)

Here is SQLFiddle demo


UPDATE2: Now your code with INSERT ... ON DUPLICATE KEY UPDATE and prepared statement can look like this

$name  = $_POST['name'];
$team  = $_POST['team'];
$num   = $_POST['number'];
$goals = $_POST['goals'];

/* connect to the database*/
$db = new mysqli('localhost', 'user', 'userpwd', 'test');
/* check connection */
if ($db->connect_errno) {
    die('Connection failed: ' .$db->connect_error);
}

$sql = 'INSERT INTO goalscorers (`name`, `team`, `num`, `goals`)
        VALUES (?, ?, ?, ?)
        ON DUPLICATE KEY UPDATE goals = goals + VALUES(goals)';
/* create a prepared statement */
if ($stmt = $db->prepare($sql)) {
    /* bind parameters for markers */
    $stmt->bind_param("ssii", $name, $team, $num, $goals);
    /* execute query */
    if ($stmt->execute()) {
        echo '<h1>Thank you for submitting your details! <br /> <a href="goalscorers.php">Add another</a></h1>';
    } else {
        die('Insert failed: ' .$db->error);    
    }
    /* close statement */
    $stmt->close();
} else {
    die('Statement prepare failed: ' .$db->error);    
}

Upvotes: 1

Related Questions