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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:13:25+00:00 2026-06-07T13:13:25+00:00

I am creating a web page where you can edit local listing sites for

  • 0

I am creating a web page where you can edit local listing sites for clients. I am using the INSERT ... ON DUPLICATE KEY UPDATE syntax in mysql and php. Below is the code:

edit-listings-exec.php

<?
    //Array to store validation errors
    $errmsg_arr = array();

    //Validation error flag
    $errflag = false;

    $member_id = $_SESSION['MEMBER_ID'];
    $userresult = mysqli_query($link, "SELECT   username 
                                    FROM        members 
                                    WHERE       member_id = '$member_id'");
    $row = mysqli_fetch_array($userresult);
    $username = $row['username'];


    //run a for loop to check for duplicates
    for ($i=1; $i < 10; $i++) {
        $selectbox = $_POST['listing'.$i];

        for ($j=1; $j < 10; $j++) {
            if ($j != $i) {
                if ($_POST['listing'.$j] === $selectbox && $_POST['listing'.$j] != "") {
                $errmsg_arr[] = "Duplicate listing source '$selectbox'";
                $errflag = true;
                break;
                }
            }
        }
    }

    //if there are input validations redirect back to main page
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: edit-listings.php");
        exit();
    }

    //run loop to define post variables
    for ($k=1; $k < 10; $k++) {
        ${'listing'.$k} = mysqli_real_escape_string($link, $_POST['listing'.$k]);
        ${'listing'.$k.'_site'} = mysqli_real_escape_string($link, $_POST['listing'.$k.'_site']);
    }


    //insert listings into local_listings table for the user
    $qry = "INSERT INTO local_listings (
                        member_id,
                        username,
                        listing1,
                        listing1_site,
                        listing2,
                        listing2_site,
                        listing3,
                        listing3_site,
                        listing4,
                        listing4_site,
                        listing5,
                        listing5_site,
                        listing6,
                        listing6_site,
                        listing7,
                        listing7_site,
                        listing8,
                        listing8_site,
                        listing9,
                        listing9_site)
                VALUES (
                        '$member_id',
                        '$username',
                        '$listing1',
                        '$listing1_site',
                        '$listing2',
                        '$listing2_site',
                        '$listing3',
                        '$listing3_site',
                        '$listing4',
                        '$listing4_site',
                        '$listing5',
                        '$listing5_site',
                        '$listing6',
                        '$listing6_site',
                        '$listing7',
                        '$listing7_site',
                        '$listing8',
                        '$listing8_site',
                        '$listing9',
                        '$listing9_site')
                ON DUPLICATE KEY UPDATE
                        listing1        =   '$listing1',
                        listing1_site   =   '$listing1_site',
                        listing2        =   '$listing2',
                        listing2_site   =   '$listing2_site',
                        listing3        =   '$listing3',
                        listing3_site   =   '$listing3_site',
                        listing4        =   '$listing4',
                        listing4_site   =   '$listing4_site',
                        listing5        =   '$listing5',
                        listing5_site   =   '$listing5_site',
                        listing6        =   '$listing6',
                        listing6_site   =   '$listing6_site',
                        listing7        =   '$listing7',
                        listing7_site   =   '$listing7_site',
                        listing8        =   '$listing8',
                        listing8_site   =   '$listing8_site',
                        listing9        =   '$listing9',
                        listing9_site   =   '$listing9_site'";

    $result = mysqli_query($link, $qry);

    if (mysqli_affected_rows($result) < 1) {
        $errmsg_arr[] = "Unable to insert listings for $username";
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        header ("location: edit-listings.php");
    }
    elseif (mysqli_affected_rows($result) === 1) {
        $_SESSION['RESULT'] = "New listings for '$username' inserted successfully";
        header ("location: edit-listings-success.php");
    }
    elseif (mysqli_affected_rows($result) === 2) {
        $_SESSION['RESULT'] = "Listings for '$username' have been updated";
        header ("location: edit-listings-success.php");
    }


?>

The data is successfully inserting into the local_listings table but mysqli_affected_rows always returns a value of 0. It should be returning a value of 1 if it inserts a new row or 2 if it simply updates. Any ideas why this is happening?

  • 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-07T13:13:26+00:00Added an answer on June 7, 2026 at 1:13 pm

    Try this:

        if (mysqli_affected_rows($link) < 1) {
    ... your code
    }
    

    take a look here

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

Sidebar

Related Questions

I'm creating a web page, and I can't (with html code) add a draw
I am creating web page using asp.net. Is it possible to remove/hide the browsers
When creating a new item - Web Form using Master Page I get a
EDIT 1: Can someone please help me create a very very simplistic sample page
My web page has auto refresh functionality. The auto refresh is implemented using a
I'm creating an app which can create, edit or view a place. When I
When creating a web page who's content will be created and destroyed dynamically by
I am creating a web page and have a section that I want to
Inside a button click handler, I'm creating a new web page like so: var
I am creating a webpage which is using asp .net as its backend. I

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.