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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:27:09+00:00 2026-06-12T06:27:09+00:00

See the code below, it check if the data exist in the table, if

  • 0

See the code below, it check if the data exist in the table, if not exist then insert it or else update the table.

As you can see it look a bit messy – is there anyway to improve the code logic or something smaller? I have a few tables that need doing same thing.

foreach ($sheet as $data) {
    // Get Phone ID
    $dataPhoneID = mysql_escape_string($data['handset']['phone_id']);

    if (isset($stocks[$dataPhoneID])) {
        $stockPhone = $stocks[$dataPhoneID ];
        $phoneName = mysql_escape_string($stockPhone['description']);
        $stock = mysql_escape_string($stockPhone['stock']);

        $SQL = "SELECT * FROM phone_affiliate WHERE affiliate_id = 1 AND affiliate_phone_id = '$dataPhoneID'";
        $q = mysql_query($SQL);
        if (mysql_num_rows($q) == 0) {
            $SQLInsert = "INSERT INTO phone (name) VALUE('$phoneName')";
            if (mysql_query($SQLInsert)) {
                $phone_id = mysql_insert_id();
                $SQLInsert = "INSERT INTO phone_affiliate (phone_id, affiliate_id, affiliate_phone_id, stock) ";
                $SQLInsert .= "VALUE('$phone_id', '1', '$dataPhoneID', '$stock')";
                mysql_query($SQLInsert) or die(mysql_error());
            }
        } else {
            $row = mysql_fetch_assoc($q);
            $phone_id = $row['phone_id'];
            $SQLUpdate = "UPDATE phone_affiliate set stock = '$stock' WHERE affiliate_id = 1 AND phone_id = $phone_id";
             mysql_query($SQLUpdate) or die(mysql_error());
        }

      // Similar code block above for other tables.
    }
}

Note: I am aware about PDO but I don’t have time to replace it on existing system.

  • 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-12T06:27:10+00:00Added an answer on June 12, 2026 at 6:27 am

    Use mysql’s REPLACE INTO or INSERT… ON DUPLICATE KEY UPDATE. For example:

    foreach ($sheet as $data) {
        // Get Phone ID
        $dataPhoneID = mysql_escape_string($data['handset']['phone_id']);
    
        if (isset($stocks[$dataPhoneID])) {
            $stockPhone = $stocks[$dataPhoneID ];
            $phoneName = mysql_escape_string($stockPhone['description']);
            $stock = mysql_escape_string($stockPhone['stock']);
    
            $SQLInsert = "INSERT INTO phone_affiliate (affiliate_id, affiliate_phone_id, stock) ";
            $SQLInsert .= "VALUES ('1', '$dataPhoneID', '$stock') ";
            $SQLInsert .= "ON DUPLICATE KEY UPDATE stock = '$stock'";
            mysql_query($SQLInsert);
            if (mysql_insert_id()) {
                $SQLInsert = "INSERT INTO phone (name) VALUE('$phoneName')";
                mysql_query($SQLInsert);
                $phone_id = mysql_insert_id();
                $SQLUpdate = "UPDATE phone_affiliate set phone_id = $phone_id WHERE affiliate_id = 1 AND affiliate_phone_id = $dataPhoneID_id";
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function(please see code below) which reads some data from the web.
I have a mysqli/php code below where it is suppose to insert data into
In my example code below, you can see that I have been trying to
See code below: I want Page 1 to alert #testing... This works in C
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
I am using vb.net code. I have grid view, please see the code below:
I've a problem with CakePHP __() function. Please see the code below : $options
How do I browse to upload a file using Dojo? See the code below:
I am having a problem using the signedCMS.decode routine. See the code below. The
Please see below code: thrust::device_vector<int>::iterator whereToBegin = copyListOfNgramCounteachdoc.begin(); end = thrust::unique_by_key(end.first, end.first + numUniqueNgrams,end.second);

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.