0

I am trying to update my third column value (option_value) with my database and code below:

database

if(isset($_POST['settings_updatebtn']))
{
    $finerate= $_POST['fine_rate'];
    $issue_email = $_POST['issue_template'];
    $return_email = $_POST['return_template'];

    $query = "UPDATE
                settings
            SET
                option_value = CASE option_name WHEN 'finerate' THEN '$finerate' WHEN 'email_temp_issue' THEN '$issue_email' WHEN 'email_temp_return' THEN '$return_email'
            WHERE
                option_name IN (
                'finerate',
                'email_temp_issue',
                'email_temp_return'
                )";
    $query_run = mysqli_query($connection,$query);

    if($query_run)
    {
        $_SESSION['success']= "Your settings is updated";
        header('Location: systemsettings.php');    }
    else
    {
        $_SESSION['status']= "Your settings is NOT updated";
        header('Location: systemsettings.php');    }
}

However it shows query not running, is my SQL statement wrong in somewhere?

5
  • you're open to SQL injection and should address imminently Commented Mar 26, 2020 at 14:56
  • what's the error you receive? Commented Mar 26, 2020 at 15:03
  • @AbhinK showing status: Your settings is NOT updated. The query did not run Commented Mar 26, 2020 at 15:26
  • @treyBake what should I write to stop the SQL injection? Commented Mar 26, 2020 at 15:49
  • @CarolH refer to here :) Commented Mar 26, 2020 at 16:14

1 Answer 1

1

use END at the end of your case statement:

UPDATE
    settings
SET
    option_value = 
        CASE option_name 
            WHEN 'finerate' THEN '$finerate' 
            WHEN 'email_temp_issue' THEN '$issue_email' 
            WHEN 'email_temp_return' THEN '$return_email'
        END
WHERE
    option_name IN (
    'finerate',
    'email_temp_issue',
    'email_temp_return'
    )
Sign up to request clarification or add additional context in comments.

1 Comment

this code is open to SQL injection and should be amended according to this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.