Updating Database Values

I’m working on a small project that uses php and an sql database. The database contains three elements - names, scores, and messages. Three variables are sent to the php file, it checks all the database ‘name’ elements for a match. If there is a match, the corresponding score and message values are replaced. If the name doesn’t exist in the database - the name, score, and comment elements are all added in a new row. This is my non working code. I can add a new entry, but my conditional statement, and UPDATE code isn’t working. I think it’s close. Any help is appreciated.

$teamName = $_POST[‘name’];
$teamScore = $_POST[‘score’];
$teamMessage = $_POST[‘message’];

if(record_exists(‘name’, $teamName))
{
$sql = “UPDATE scoring SET score=’$teamScore’, comment=’$teamMessage’ WHERE name=’$teamName’”;
}
else
{
$sql = mysqli_query($con, “INSERT INTO scoring (name, score, comment)VALUES(’$teamName’,’$teamScore’,’$teamMessage’)”);
}

Set a unique index on the column(s) and then do a single INSERT IGNORE query.

Sponsor our Newsletter | Privacy Policy | Terms of Service