MySQL on duplicate key update issues

so i am trying to setup a script to transfer over data from 1 table to another and i cannot seem to get it to work… any ideas ?
Line 4 is the issue i know just not sure how to write it using the “?” value

[php]echo "Converting “.$fromprefix.”_admins… ";
ob_flush();
$res = $olddb->Execute(“SELECT username, level FROM “.$fromprefix.”_webadmins”);
$ins = $newdb->Prepare(“INSERT INTO “.$toprefix.”_admins(user,name,gid) VALUES (?,?,?) ON DUPLICATE KEY UPDATE (user,name,gid) VALUES (?,?,?)”);
while (!$res->EOF)
{
$newdb->Execute($ins,array($res->fields[0],$res->fields[0],$levelconvert[$res->fields[1]]));
$res->MoveNext();
}
echo “OK
”;[/php]

Keep getting

Input Array does not match ?: INSERT INTO $prefix_admins(user,name,gid) VALUES ('user','name','gid') ON DUPLICATE KEY UPDATE (user,name,gid) VALUES ( You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(user,name,gid) VALUES (' at line 1

You are trying to insert two columns of data into tree columns. You have selected username, and level, but you are trying to also insert gid which you did not select.

gid has a default value, my main issue is here
[php]$ins = $newdb->Prepare(“INSERT INTO “.$toprefix.”_admins(user,name,gid) VALUES (?,?,?) ON DUPLICATE KEY UPDATE (user,name,gid) VALUES (?,?,?)”);[/php]

i know i don’t have the “ON DUPLICATE KEY UPDATE” setup right and i need help with that. the rest works fine.

You didnt say if you updated your select as I told you… If you did you should have a different error message if there is any.

I can help you easier if you post a table structure sql so I can recreate your table exactly as you have it.

Here is a working duplicate insert. The first gorilla goes straight in since there is no record with id 4. The second gorilla insert would be a duplicate insert.

[code]CREATE TABLE ins_duplicate (id INT PRIMARY KEY, animal VARCHAR(30));
INSERT INTO ins_duplicate VALUES (1,‘Aardvark’), (2,‘Cheetah’), (3,‘Zebra’);

INSERT INTO ins_duplicate VALUES (4,‘Gorilla’) ON DUPLICATE KEY UPDATE animal=‘Gorilla’;

INSERT INTO ins_duplicate VALUES (4,‘Gorilla’) ON DUPLICATE KEY UPDATE animal=‘Gorilla’;[/code]

i did fix a slight oversight that was in the field names now its taking the correct information and putting it in the correct fields.

the problem i am having is i’m not sure how to use :
ON DUPLICATE KEY UPDATE (user,password,gid,email) VALUES b[/b]
since the correct syntax is:
ON DUPLICATE KEY UPDATE field1=value1, field2=value2;

Here is the code in full
[php]<?php
define(‘IN_SB’, true);
require_once("…/config.php");

include_once("../includes/adodb/adodb.inc.php");
include_once("../includes/adodb/adodb-errorhandler.inc.php");


function convertAmxbans($fromdsn,$todsn,$fromprefix,$toprefix)
{
	set_time_limit(0); //Never time out
	ob_start();
	$olddb = ADONewConnection($fromdsn);
    if (!$olddb)
    {
        die("Failed to connect to AMX Bans database");
    }
   	$olddb->Execute("SET NAMES utf8");

   	echo "Converting ".$fromprefix."_bans... ";
   	ob_flush();
	flush();
	$res = $olddb->Execute("SELECT `player_ip`, `player_id`, `player_nick`, `ban_created`, `ban_length`, `ban_reason`, `admin_ip` FROM `".$fromprefix."_bans`");
	$time = $olddb->GetRow("SELECT UNIX_TIMESTAMP() AS time FROM ".$fromprefix."_bans");
	
	$newdb = ADONewConnection($todsn);
	 if (!$newdb)
    {
        die("Failed to connect to SourceBans database");
    }
	$newdb->Execute("SET NAMES utf8");
	$ins = $newdb->Prepare("INSERT INTO ".$toprefix."_bans(ip,authid,name,created,ends,length,reason,adminIp,aid) VALUES (?,?,?,?,?,?,?,?,?)");
	
	while (!$res->EOF)
	{
    	$vals = array($res->fields[0],$res->fields[1],$res->fields[2],$res->fields[3],($res->fields[4] == 0 ? 0 : $res->fields[3]+$res->fields[4]),
						$res->fields[4],$res->fields[5],$res->fields[6],0);
		
		if($time > (intval($res->fields[3]) + intval($res->fields[4])) && $res->fields[4] != 0)
		{
			$res->MoveNext();
			continue;
		}
			
		foreach ($vals as $ind=>$cur)
		{
    		if (is_null($cur))
    		{
        		$vals[$ind] = '';
    		}
		}
		$newdb->Execute($ins,$vals);
		$res->MoveNext();
	}
	echo "OK<br>";

	echo "Converting ".$fromprefix."_banhistory... ";
	ob_flush();
	$res = $olddb->Execute("SELECT player_ip, player_id, player_nick, ban_created, ban_length, ban_reason, admin_ip, admin_id, admin_nick
				,server_ip, server_name, unban_created FROM ".$fromprefix."_banhistory");
	$ins = $newdb->Prepare("INSERT INTO ".$toprefix."_banhistory(Type,ip,authid,name,created,ends,length,reason,adminIp,Adminid,RemovedOn,RemovedBy) VALUES ('U',?,?,?,?,?,?,?,?,?,?,?)");
	while (!$res->EOF)
	{
    	$vals = array($res->fields[0],$res->fields[1],$res->fields[2],$res->fields[3],($res->fields[4] == 0 ? 0 : $res->fields[3]+$res->fields[4])
			,$res->fields[4],$res->fields[5],$res->fields[6],$res->fields[7],$res->fields[8],$res->fields[7]);
		
		foreach ($vals as $ind=>$cur)
		{
    		if (is_null($cur))
    		{
        		$vals[$ind] = '';
    		}
		}
		$newdb->Execute($ins,$vals);
		$res->MoveNext();
	}
    echo "OK<br>";
    
echo "Converting ".$fromprefix."_levels... ";
	ob_flush();
	$res = $olddb->Execute("SELECT level, bans_add, bans_edit, bans_delete, bans_unban, bans_import, bans_export, amxadmins_view, amxadmins_edit
	            , webadmins_view, webadmins_edit, permissions_edit, servers_edit FROM ".$fromprefix."_levels");
	$ins = $newdb->Prepare("INSERT INTO ".$toprefix."_groups(type,name,flags) VALUES (1,?,?)");
	$levelconvert = array();
	while (!$res->EOF)
	{
    	$acc = 0;
    	if ($res->fields[1] == 'yes' || $res->fields[2] == 'yes' || $res->fields[3] == 'yes' || $res->fields[4] == 'yes')
    	{
        	$acc |= ADMIN_WEB_BANS;
    	}
    	// amxadmins_view is ignored
    	if ($res->fields[6] == 'yes')
    	{
        	$acc |= ADMIN_SERVER_ADMINS;
    	}
    	// webadmins_view is ignored
    	if ($res->fields[8] == 'yes')
    	{
        	$acc |= ADMIN_WEB_AGROUPS;
    	}
    	if ($res->fields[9] == 'yes')
    	{
        	$acc |= ADMIN_WEB_AGROUPS | ADMIN_SERVER_AGROUPS;
    	}
    	if ($res->fields[10] == 'yes')
    	{
        	$acc |= ADMIN_SERVER_ADD | ADMIN_SERVER_REMOVE | ADMIN_SERVER_GROUPS;
    	}
    	if ($res->fields[0] == '1')
    	{
        	$acc |= ADMIN_OWNER;
    	}
      	$newdb->Execute($ins,array("AMXBANS_".$res->fields[0],$acc));   	
        $levelconvert[$res->fields[0]] = $newdb->Insert_ID();
    	$res->MoveNext();	
	}
	echo "OK<br>";
	
	echo "Converting ".$fromprefix."_admins... ";
	ob_flush();
	$res = $olddb->Execute("SELECT username, password, level, email FROM ".$fromprefix."_webadmins");
	$ins = $newdb->Prepare("INSERT INTO ".$toprefix."_admins(user,password,gid,email) VALUES (?,?,?,?)");
	while (!$res->EOF)
	{
    	$newdb->Execute($ins,array($res->fields[0],$res->fields[1],$levelconvert[$res->fields[2]],$res->fields[3]));
    	$res->MoveNext();
	}
	echo "OK<br>";

?>[/php]

im converting from this table:

CREATE TABLE IF NOT EXISTS `_webadmins` ( `id` int(12) NOT NULL AUTO_INCREMENT, `username` varchar(32) DEFAULT NULL, `password` varchar(32) DEFAULT NULL, `level` int(11) DEFAULT '99', `logcode` varchar(64) DEFAULT NULL, `email` varchar(64) DEFAULT NULL, `last_action` int(11) DEFAULT NULL, `try` int(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`,`email`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

to this table

CREATE TABLE IF NOT EXISTS `_admins` ( `aid` int(6) NOT NULL auto_increment, `user` varchar(64) NOT NULL, `authid` varchar(64) NOT NULL default '', `password` varchar(128) DEFAULT 'XXXXXXXXXX', `gid` int(6) NOT NULL, `email` varchar(128) DEFAULT '[email protected]', `validate` varchar(128) NULL default NULL, `extraflags` int(10) NOT NULL, `immunity` int(10) NOT NULL default '0', `srv_group` varchar(128) default NULL, `srv_flags` varchar(64) default NULL, `srv_password` varchar(128) default NULL, `lastvisit` int(11) NULL, PRIMARY KEY (`aid`), UNIQUE KEY `user` (`user`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Your function is missing a closing bracket

[php]function convertAmxbans($fromdsn,$todsn,$fromprefix,$toprefix)
{[/php]

Do you have some sample data for your table that you are converting from? If you can, provide a few insert statements from your first table.

When you run it now, what errors are you getting? If you want to provide me with a temporary login we can handle this much faster and easier. Send me a private message with the info.

i gave up on this pursuit, and started a new approach … thanks for the help …

Sponsor our Newsletter | Privacy Policy | Terms of Service