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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:30:52+00:00 2026-05-15T04:30:52+00:00

I think that (the title) is the problem I am having. I set up

  • 0

I think that (the title) is the problem I am having. I set up a MySQL connection, I read an XML file, and then I insert those values into a table by looping through the elements. The problem is, instead of inserting only 1 record, sometimes I insert 2 or 3 or 4. It seems to depend on the previous values I have read. I think I am reinitializing the variables, but I guess I am missing something — hopefully something simple.

Here is my code. I originally had about 20 columns, but I shortened the included version to make it easier to read.

$ctr = 0;
$sql =  "insert into csd (id,type,nickname,hostname,username,password) ".
        "values (?,?,?,?,?,?)";
$cur = $db->prepare($sql);

for ($ctr = 0; $ctr < $expected_count; $ctr++) {
    unset($bind_vars,$dat);

    $lbl = "csd_{$ctr}";

    $dat['type']      = (string) $ref->itm->csds->$lbl->type;
    $dat['nickname']  = (string) $ref->itm->csds->$lbl->nickname;
    $dat['hostname']  = (string) $ref->itm->csds->$lbl->hostname;
    $dat['username']  = (string) $ref->itm->csds->$lbl->username;
    $dat['password']  = (string) $ref->itm->csds->$lbl->password;

    $bind_vars = array( $id,$dat['$type'], $dat['$nickname'], $dat['$hostname'], 
                        $dat['$username'], $dat['$password']);
    print_r ($bind_vars);
    $res = $db->execute($cur, $bind_vars);
}

P.S. I also tagged this SimpleXML because that is how I am reading the file, though that code is not included above. It looks like this:

$ref = simplexml_load_file($file);

UPDATE: I’ve changed the code around as per suggestions, and now it is not always the same pattern, but it is equally broken. When I display the bind array before inserting, it looks like this. Note that I also count the rows before and after, so there are 0 rows, then I insert 1, then there are 2:

0 CSDs on that ITEM now.
Array
(
    [0] => 2
    [1] => 0
    [2] =>
    [3] => X
    [4] => XYZ
    [5] =>
    [6] =>
    [7] =>
    [8] => audio
    [9] =>
    [10] => 192.168.0.50
    [11] => 192.168.0.3
    [12] => 255.255.255.0
    [13] => 255.255.255.0
    [14] =>
    [15] =>
    [16] =>
    [17] => 21
    [18] => 5
    [19] => Y
    [20] => /dir
)
2 CSDs on that ITEM now.
  • 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-15T04:30:52+00:00Added an answer on May 15, 2026 at 4:30 am

    not sure the exact answer but this maybe help

    $ctr = 0;
    $sql =  "insert into csd (id,type,nickname,hostname,username,password) ".
            "values (?,?,?,?,?,?)";
    $cur = $db->prepare($sql);
    
    for ($ctr = 0; $ctr < $expected_count; $ctr++) {
    
        //list (  $lbl, $type, $nickname, $hostname, $username, $password) = "";
        //$bind_vars = array();
        // use unset
        unset($bind_vars,$dat);
    
        $lbl = "csd_{$ctr}";
    
        $dat['type']      = $ref->itm->csds->$lbl->type;
        $dat['nickname']  = $ref->itm->csds->$lbl->nickname;
        $dat['hostname']  = $ref->itm->csds->$lbl->hostname;
        $dat['username']  = $ref->itm->csds->$lbl->username;
        $dat['password']  = $ref->itm->csds->$lbl->password;
    
        $bind_vars = array($id,$dat['$type'],$dat['$nickname'],$dat['$hostname'],$dat['$username'],$dat['$password']);
        $res = $db->execute($cur, $bind_vars);
    
        # this is a separate function which works, but which only 
        # does SELECTS and cannot be the problem.  I include it because I 
        # want to count the total rows.
    
        printf ("%d CSDs on that ITEM now.\n",  CountCSDs($id_to_sync));
    }
    

    or maybe you should check/echo $expected_count value first to determine correct count

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

Sidebar

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.