Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5977585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:23:31+00:00 2026-05-22T21:23:31+00:00

I use: INSERT INTO `rating` (`name`, `user`, `rating`, `section`, `ip`) VALUES (‘$name’, ‘{$_SESSION[‘user’]}’, ‘$rate’,

  • 0

I use:

INSERT INTO `rating` (`name`, `user`, `rating`, `section`, `ip`)
VALUES ('$name', '{$_SESSION['user']}', '$rate', '$section',
        '{$_SERVER['REMOTE_ADDR']}');";

I would like to add an if condition in the IF statement so that.

IF SELECT ip from rating 
where ip={$_SERVER['REMOTE_ADDR']} AND section=$section AND name=$name 
then update ELSE INSERT new row

is it doable or I better code it in php ?
thank you very much

P.s: I know how to do it with php, I want to learn it with MySQL.

Also i require that all 3 name,section,ip matchs not only ip

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:23:32+00:00Added an answer on May 22, 2026 at 9:23 pm

    To expand on Eric’s excellent answer.

    To add a unique constraint on each of the columns ip, name, section run the following code on the database

    ALTER TABLE `test`.`rating` 
      ADD UNIQUE INDEX `name`(`name`),
      ADD UNIQUE INDEX `section`(`section`),
      ADD UNIQUE INDEX `ip`(`ip`);
    

    To run a unique constraint on the combination of columns ip+name+section do:

    ALTER TABLE `test`.`rating` 
      ADD UNIQUE INDEX `name_section_ip`(`name`, `section`, `ip`);
    

    The last thing is probably what you want.

    One last thing
    I’m not 100% sure, but this usage of {$_SERVER['REMOTE_ADDR']} in the query does not look SQL-injection safe.
    Consider changing it into:

    $remote_adr = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
    $session = mysql_real_escape_string($_SESSION['user']);
    $query = "INSERT INTO `rating` (`name`, `user`, `rating`, `section`, `ip`)
      VALUES ('$name', '$session', '$rate', '$section','$remote_adr')";
    

    Finally putting a “;” in a mysql_query() like so mysql_query("select * from x;"); does not work mysql_query() will only ever execute one query and will reject your ;.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how would I use the insert into syntax with mysql columns that are auto
What commands in Emacs can I use to insert into the text buffer of
In Informix I can run SQL statement that use other database: INSERT INTO other_db:mytable
I have the following code I use to insert form data into a single
I want to use javascript to insert some elements into the current page. Such
I have below code to insert a style into DOM (there is a use
I'd like to use a foreach loop to insert a _POST associative array into
Is it possible to use LAST_INSERT_ID() within an insert query? INSERT INTO mytable (col1,
I use an INSERT INTO & DELETE FROM combination in a PHP script to
Although there is no reason (apart maybe from aesthetics) to use INSERT INTO SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.