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 9088219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:52:12+00:00 2026-06-16T21:52:12+00:00

I want to insert a record if it does not exist without using unique

  • 0

I want to insert a record if it does not exist without using unique ID, and I found this answer here: MySQL: Insert record if not exists in table.

In my case I have a table:

+----+---------+---------+---------+---------+-----------+-------------+-----------+
| ID | dRelIns | vRelIns | dRelDel | vRelDel | cRelAktiv | iRelDateiId | iRelKatId |
+----+---------+---------+---------+---------+-----------+-------------+-----------+
| 1  |  blabla |  blabla |  NULL   |  NULL   |    J      |      3      |     5     |
+----+---------+---------+---------+---------+-----------+-------------+-----------+
| 2  |  blabla |  blabla |  blabla |  blabla |    N      |      3      |     1     |
+----+---------+---------+---------+---------+-----------+-------------+-----------+
| 3  |  blabla |  blabla |  NULL   |  NULL   |    J      |      3      |     2     |
+----+---------+---------+---------+---------+-----------+-------------+-----------+

I am getting an array ($_POST) with id iRelKatId and another on id iRelDateiId. I check if the id already exists and cRelAktiv = ‘J’, if not I want to insert a new one. If some entry exists but it’s not in the list, I want to set cRelAktiv to ‘N’.

My PHP script with the SQL queries:

$list=implode("','", $_POST["kat"]);
        $sql="UPDATE tabRel_UDK SET dRelDel=NOW(),
                                    vRelDel=USER(),
                                    cRelAktiv='N'
                                WHERE iRelDateiId='$_POST[id]' AND cRelAktiv='J' AND iRelKatId NOT IN('$list')";
        $result =  mysql_query($sql) or die(Error (" . mysql_error() . ").");

        foreach ($_POST["kat"] as $value) {
            $sql="INSERT INTO tabRel_UDK (dRelIns, vRelIns, cRelAktiv, iRelDateiId, iRelKatId)
                SELECT * FROM (SELECT NOW(), USER(), 'J', '$_POST[id]','$value') AS tmp
                WHERE NOT EXISTS (
                SELECT iRelDateiId,iRelKatId,cRelAktiv FROM tabRel_UDK 
                WHERE iRelDateiId = '$_POST[id]' AND iRelKatId='$value' AND cRelAktiv='J') LIMIT 1;";
            $result =  mysql_query($sql) or die("Error (" . mysql_error() . ").");
        }

This script works for me, but when both ids have the same value(for example 5), it throws an error Duplicate column name ‘5’ because of SELECT * FROM (SELECT NOW(), USER(), 'J', '$_POST[id]','$value')

Any ideas how to make it works, or should I make 2-3 SQL queries and check the ids manually in PHP?

  • 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-06-16T21:52:12+00:00Added an answer on June 16, 2026 at 9:52 pm

    I suspect $_POST[id] and $value have the same value, and so you appear to be selecting the same column twice. This should be suppressed in code, so you only select it once – or you should be giving each an alias so this does not happen.

    I wouldn’t recommend returning a resultset with column names starting with a number anyway – in some database systems that would not be permitted, unless it is quoted. Give them string prefixes as aliases, ending in _<number> if you must.

    Thus, your subselect string might look like this:

    "SELECT * FROM (
        SELECT
            NOW(),
            USER(),
            'J',
            '{$_POST['id']}' AS val_1,
            '{$value}' AS val_2
    ) WHERE ..."
    

    More importantly, having $_POST[id] in your code will open you up to SQL injection vulnerabilities – always escape data before using it. Even better, switch to MySQL PDO and use parameterisation.

    Lastly, the variable should be $_POST['id'] – PHP assumes that you meant a string index, but it will raise a warning if you skip the quotes. Turn on warnings so you can see mistakes like this.

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

Sidebar

Related Questions

I want to insert a record into a MySQL database table from a formulaire.
I want to insert a date record to an Access database. Here is my
Possible Duplicate: insert contacts into database but does not want to duplicate already existing
I have a need to do an insert to MySQL where a record does
I want to have an 'updateinfo' table in order to record every update/insert/delete operations
I want to insert the following include tag into my webpage using JavaScript. <!--#include
I want to insert some items to mysql db. When I did it in
I want to insert some phrases to MySQL the first field is english and
I have this Tags table CREATE TABLE IF NOT EXISTS `Tags` ( `id_tag` int(10)
I am looking for a way to only insert when the row does not

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.