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

  • SEARCH
  • Home
  • 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 6128127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:35:30+00:00 2026-05-23T16:35:30+00:00

I am VERY new to mySQL, please bear with me. I have an array

  • 0

I am VERY new to mySQL, please bear with me.

I have an array that is always growing, lets say for now it has 4 items in it. I need the sql to search the table and insert a new row for each item in the array, but only if its a new item (meaning it hasn’t already been inserted). There are 5 columns in the table, and each item in the row needs to be a variable so i can pass dynamic data through.

I know this is way off but what i have now is something like this.

$array_count = array(1,2,3,4);
$var1 = 'value 1';
$var2 = 'value 2';
$var3 = 'value 3';
$var4 = 'value 4';
$var5 = 'value 5';
$sql = "INSERT INTO membership_levels VALUES ('".$var1."', '".$var2."', '".$var3."', '".$var4."', '".$var5."')";
  • 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-23T16:35:30+00:00Added an answer on May 23, 2026 at 4:35 pm

    Mysql – ON DUPLICATE KEY

    If you database is MySql you can use like Brad Christie has wrote with the Insert On Duplicate. You will need to have a primary key on the 5 columns if you want inserting only if they are all the same value.

    Your $sql will change to :

    $sql = "INSERT INTO membership_levels(a,b,c,d) VALUES VALUES ('".$var1."', '".$var2."', '".$var3."', '".$var4."', '".$var5."')
                   ON DUPLICATE KEY UPDATE a=VALUES(a), b=VALUES(b), c = VALUES(c), d = VALUES (d);"
    

    Mysql – Replace

    You can also use the Replace key word. If the row exist, it will delete it for you and insert the new one.

    $sql = "REPLACE INTO membership_levels VALUES ('".$var1."', '".$var2."', '".$var3."', '".$var4."', '".$var5."')";
    

    MsSql

    If you database is MsSql (Sql server) it’s 2 queries :

    IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
        UPDATE Table1 SET (...) WHERE Column1='SomeValue'
    ELSE
        INSERT INTO Table1 VALUES (...)"
    

    The best way if SQL Server would be to use Stored Procedure.

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

Sidebar

Related Questions

I'm very new to MySQL and I have a stored procedure that I'd like
I'm very new to MySQL so I need some help please, I have a
Before reading, please note that I am very new to both PHP and MYSQL.
I am very new to mysql and php. I need to put an array
I am very new to Indexes in MySQL. I know, I should probably have
Hy! I am very new in Web-Dev. I have a mysql db and the
I am very, very new to MYSQL.I tried to create a table named option.
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
I very new to Python, and fairly new to regex. (I have no Perl
I still very new using Subversion. Is it possible to have a working copy

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.