Jump to content

[SOLVED] Checking the results from my MySQL table against my parsed XML file


wtfsmd

Recommended Posts

I am trying to check the results from my MySQL table against my parsed XML file. What i want to do is if the two match i want to run the Update script, if they do not match i want to run the Insert script.

I don't want to use Truncate or Delete.

Everything i have tried has not worked and i was wondering if anyone could give a few examples, here is one of the ways i was trying to go about this.

(ill spare you all of the parsing code)

<?php

function start_tag($parser, $name, $attribs) {
   global $current;
   $current = $name;

   if (is_array($attribs)) {
      while(list($key,$val) = each($attribs)) {
        if ($name == "CHARACTER") { 
         if ($key == "NAME") { $char_name = $val; }
         if ($key == "LEVEL") { $level = $val; }
         if ($key == "CLASSID") { $class = $val;}
         if ($key == "GENDERID") { $gender = $val; }
         if ($key == "RACEID") { $race = $val; }
         if ($key == "RANK") { $rank = $val; }
        } 
       }

        $query = "SELECT * FROM roster"; 
        $result = mysql_query($query) or die(mysql_error());
        while($row = mysql_fetch_array($result)){
            $check_name = $row['name'];
            if($check_name == $char_name){ $check = "yes";}else{ $check = "no";}
        }
        
        
        if($check == "yes") {
            // Insert the data from the XML file into the table "ROSTER"
               mysql_query("UPDATE roster SET level='$level', class='$class', gender='$gender', race='$race', rank='$rank' WHERE name='$char_name'")
              or die(mysql_error());
        } 
        elseif($check == "no") {
             mysql_query("INSERT INTO roster 
              (name, level, class, gender, race, rank) VALUES
              ('$char_name', '$level', '$class', '$gender', '$race', '$rank' ) ") 
              or die(mysql_error());
        }
    }
}

?>

Link to comment
Share on other sites

Okay i used the following i do not get any errors and it doesn't work, what am i missing?

<?php
function start_tag($parser, $name, $attribs) {
   global $current;
   $current = $name;

   if (is_array($attribs)) {
      while(list($key,$val) = each($attribs)) {
	if ($name == "CHARACTER") { 
		if ($key == "NAME") { $char_name = $val; }
		if ($key == "LEVEL") { $level = $val; }
		if ($key == "CLASSID") { $class = $val;}
		if ($key == "GENDERID") { $gender = $val; }
		if ($key == "RACEID") { $race = $val; }
		if ($key == "RANK") { $rank = $val; }
	} 
       }

	mysql_query("INSERT INTO roster 
	(name, level, class, gender, race, rank) VALUES ('$char_name', '$level', '$class', '$gender', '$race', '$rank' ) ON DUPLICATE KEY UPDATE level='$level', class='$class', gender='$gender', race='$race', rank='$rank', name='$char_name'")
	or die(mysql_error());

    }
}
?>

Link to comment
Share on other sites

Do

echo $query = "INSERT INTO roster 
      (name, level, class, gender, race, rank) VALUES ('$char_name', '$level', '$class', '$gender', '$race', '$rank' ) ON DUPLICATE KEY UPDATE level='$level', class='$class', gender='$gender', race='$race', rank='$rank'";

 

To see if your query is well formed. You might even try to feed it into phpMyAdmin or other tool to see if it works.

 

Is `name` column declared as UNIQUE in `roster`?

 

Link to comment
Share on other sites

I figured it out and it works now, apparently i made a noob move. Here was the fix:

 

Here is my old code

<?php
mysql_query("INSERT INTO roster 
      (name, level, class, gender, race, rank) VALUES ('$char_name', '$level', '$class', '$gender', '$race', '$rank' ) ON DUPLICATE KEY UPDATE level='$level', class='$class', gender='$gender', race='$race', rank='$rank', name='$char_name'")
      or die(mysql_error());
?>

 

Here is the fixed code

<?php
mysql_query("INSERT INTO roster 
      (name, level, class, gender, race, rank) VALUES ('$char_name', '$level', '$class', '$gender', '$race', '$rank' ) ON DUPLICATE KEY UPDATE name='$char_name', level='$level', class='$class', gender='$gender', race='$race', rank='$rank'")
      or die(mysql_error());
?>

 

It didn't work when i had the (name='$char_name') on the end but when i switched it around it works.

Thanks so much for you help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.