Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: The Wizard - helmikuu 26, 2014, 07:48:35 AP

Otsikko: how to fix this?
Kirjoitti: The Wizard - helmikuu 26, 2014, 07:48:35 AP
Error:
LainaaDuplicate entry '501' for key 1

Code with Error:

global $smcFunc, $db_prefix;

$category = 501;

// ----- Do not chnage any settings below -----

$cats = array();

$result = $smcFunc['db_query']('', "
SELECT id
FROM {db_prefix}shop_categories", array());

$row = $smcFunc['db_fetch_assoc']($result);

$smcFunc['db_free_result']($result);


if ($row != $category)
{
$smcFunc['db_insert']('insert', '{db_prefix}shop_categories',
array('name' => 'string', 'id' => 'int', 'count' => 'int'),
array('name' => 'Companions', 'id' => $category, 'count' => '12'),
array('name' => 'id', 'type' => 'primary', 'columns' => array('id') ));
}


Note: sometimes the category 501 will exist sometimes not (admin choice). So I want to add the category if it does not exist and if it does I want to ignore adding the category. I expect the answer is very, very simple and so I can't see it.

Otsikko: Re: how to fix this?
Kirjoitti: margarett - helmikuu 26, 2014, 09:35:07 AP
You can choose to:
['db_insert']('insert') --> inserts a new record and demands unique register, the error you're having
['db_insert']('replace') --> will replace the existing register if it exists, insert it if not (careful, the replacement is actually a delete + insert, meaning it will get a new id)
['db_insert']('ignore') --> will insert the register if it doesn't exist, ignore the request if it does

(not totally sure, actually :P I was just looking through code and google)
Otsikko: Re: how to fix this?
Kirjoitti: The Wizard - helmikuu 26, 2014, 11:21:29 AP
Thats what I needed- ['db_insert']('ignore') knew it was something simple.

Thanks margarett
Otsikko: Re: how to fix this?
Kirjoitti: vbgamer45 - helmikuu 26, 2014, 11:21:46 AP
Also that if ($row != $category) is not right
You should be checking for an exact id in your last sql statement then check $row['id']
Otsikko: Re: how to fix this?
Kirjoitti: The Wizard - helmikuu 26, 2014, 12:04:52 IP
Finished Version. Feel free to look it over and point out any mistakes.

Wiz


global $smcFunc, $db_prefix;

$category = 501;

// ----- Do not chnage any settings below -----

$smcFunc['db_insert']('ignore', '{db_prefix}shop_categories',
array('name' => 'string', 'id' => 'int', 'count' => 'int'),
array('name' => 'Companions', 'id' => $category, 'count' => '12'),
array('name' => 'id', 'type' => 'primary', 'columns' => array('id') ));


// ***** Doctor Who Companions Package 1 *****

$smcFunc['db_insert']('insert', '{db_prefix}shop_items',
array('name' => 'string', 'desc' => 'string', 'price' => 'float', 'module' => 'string', 'stock' => 'int', 'image' => 'string',
'info1' => 'int', 'info2' => 'int', 'input_needed' => 'int', 'can_use_item' => 'int', 'delete_after_use' => 'int',
'category' => 'int' ),

array(
array('name' => 'Amy Pond', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Amy Pond', 'stock' => 50,
'image' => 'amy01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Clara Oswald', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Clara Oswald', 'stock' => 50,
'image' => 'clara01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Donna Noble', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Donna Noble', 'stock' => 50,
'image' => 'dona01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Jack Harkness', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Jack Harkness', 'stock' => 50,
'image' => 'jack01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'K-9', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'K-9', 'stock' => 50,
'image' => 'k901.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'New K-9', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'New K-9', 'stock' => 50,
    'image' => 'k902.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Martha Jones', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Martha Jones', 'stock' => 50,
'image' => 'martha01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Mickey Smith', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Mickey Smith', 'stock' => 50,
'image' => 'mickey02.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'River Song', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'River Song', 'stock' => 50,
'image' => 'river01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Rory Williams', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Rory Williams', 'stock' => 50,
'image' => 'rory01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Rose Tyler', 'desc' => 'Doctor Who', 'price' => 50.00, 'module' => 'Rose Tyler', 'stock' => 50,
'image' => 'rose01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),

array('name' => 'Sarah Jane', 'desc' => 'Doctor Who', 'price' => 50, 'module' => 'Sarah Jane Smith', 'stock' => 50,
'image' => 'sarajane01.png', 'info1' => 0, 'info2' => 0, 'input_needed' => 1, 'can_use_item' => 1,
'delete_after_use' => 1, 'category' => $category ),
),
array());
Otsikko: Re: how to fix this?
Kirjoitti: margarett - helmikuu 26, 2014, 12:12:12 IP
You have a typo :P
Lainaa// ----- Do not chnage any settings below -----
Otsikko: Re: how to fix this?
Kirjoitti: The Wizard - helmikuu 26, 2014, 12:23:06 IP
typo fixed. Does anybody have a idea how to streamline the code any?
Otsikko: Re: how to fix this?
Kirjoitti: IchBin™ - helmikuu 26, 2014, 10:13:05 IP
You are doing two inserts. There's nothing to streamline really.
Otsikko: Re: how to fix this?
Kirjoitti: The Wizard - helmikuu 27, 2014, 04:08:58 AP
Thanks IchBin

just checking - there is always room for improvement.