wpdb->query prepare doesn't work - php

I followed the introduction to use prepare to insert some data to the table.
The code is like this:
$wpdb->query(
$wpdb->prepare(
"INSERT INTO {$wpdb->prefix}awpcp_adphotos VALUES
(ad_id, image_name, disabled, isprimary)
(%d, %s, %d,%d)",
$ad_id,
$ad_img,
0,
0));
Also, disabled and is_primary should be Boolean. But I am not sure what the data type in here.
I also tried
$wpdb->insert(
'{$wpdb->prefix}awpcp_adphotos', // Table name
array(
'ad_id' => $ad_id,
'image_name' => $ad_img,
'disabled' => 0,
'is_primary' => 0,
), // Columns
array(
'%s',
'%d',
'%d',
'%d'
) // Explicit formatting
);
But haven't got any luck yet.

Shouldn't it be (?) :
$wpdb->query(
$wpdb->prepare(
"INSERT INTO {$wpdb->prefix}awpcp_adphotos(`ad_id`, `image_name`, `disabled`, `isprimary`)
VALUES(%d, %s, %d,%d)",
$ad_id, $ad_img, 0, 0)
);
I think you wrote the entire thing wrong, maybe by mistake :)
For your edit, if using insert() should be like:
$wpdb->insert($wpdb->prefix.'awpcp_adphotos', array(
'ad_id' => $ad_id,
'image_name' => $image_name,
'disabled' => $disabled,
'isprimary' => $isprimary,
));

Related

PDO is inserting "null" for empty values

I am using PDO for writing to sqlite database.
// $dbo is an instance of PDO
$query = "INSERT INTO items (id, name, rank) VALUES (:id, :name, :rank)";
$statement = $dbo->prepare($query);
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => '');
$statement->execute($values);
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);
$statement->execute($values);
After executing any of these, I expect "rank" to be an empty string or even a NULL is acceptable.
But what I get in the database is 'null', yes a string 'null' value, not the real NULL.
Could not find any solution after several attempts.
Try sending NULL directly in this line
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);

Handling duplicate keys in wordpress database

I'm want to write a function that saves a user data array to a custom table in my wp database. I have this working with this function.
function insert_player($playerArray){
global $wpdb;
$table_name = $wpdb->prefix . "hots_logs_plugin";
$wpdb->insert(
$table_name,
array(
'name' => $playerArray['name'],
'player_id' => $playerArray['pid'],
'hl_mmr' => $playerArray['heroLeague'],
'qm_mmr' => $playerArray['quickMatch'],
'comb_hero_level' => $playerArray['combLevel'],
'total_games_played' => $playerArray['totalGames']
)
);
}
Now I want to extend this function so that if a duplicate player_id is sent the function updates the all the cells in the row apart from the name and player_id. The player_id is a UNIQUE KEY.
I have come up with this function, which I can't seem to get to work, and to be honest is a little over my head..
function input($playerArray){
global $wpdb;
$table_name = $wpdb->prefix . "hots_logs_plugin";
$sql = "INSERT INTO $table_name(player_id, name, hl_mmr, qm_mmr, comb_hero_level, total_games_played)
VALUES(%d,%s,%s,%s,%s,%s)
ON DUPLICATE KEY UPDATE(hl_mmr = %s, qm_mmr = %s, comb_hero_level = %s, total_games_played = %s)";
$sql = $wpdb->prepare(
$playerArray['pid'],
$playerArray['name'],
$playerArray['heroLeague'],
$playerArray['quickMatch'],
$playerArray['combLevel'],
$playerArray['totalGames']
);
$wpdb->query($sql);
}
I just need a nudge in the right direction.
OK, so I was really over doing it in my first attempt. The answer is actually really easy.
I just modified the original method to use $wpdb->replace() rather than $wpdb->insert()
The following method works great.
function insert_player($playerArray){
global $wpdb;
$table_name = $wpdb->prefix . "hots_logs_plugin";
$wpdb->replace(
$table_name,
array(
'player_id' => $playerArray['pid'],
'name' => $playerArray['name'],
'hl_mmr' => $playerArray['heroLeague'],
'qm_mmr' => $playerArray['quickMatch'],
'comb_hero_level' => $playerArray['combLevel'],
'total_games_played' => $playerArray['totalGames']
),
array (
'%d',
'%s',
'%s',
'%s',
'%s',
'%s'
)
);
}

adding multiple rows in mysql table based on an array

I have a form which allows people to message each other, a user can select multiple people to message, when i submit a form it gives me the name and id of people selected to message in an array. uptil here i am able to get it to work for a single recipient
I want to be able to use this array and INSERT message for each user in different rows of mysql table
this is the array that i get when i submit a form
Array (
[to_user_id] => Array
(
[0] => 54
[1] => 55
)
[subject] => aaa
[message] => bbb
[send_message] =>
this is the part of code that works for a single recipient but not multiple
$to_user_id_array = ($_POST['to_user_id']);
$params = array(
':to_user_id' => $to_user_id_array,
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':sender_id' => $this->user_id,
':status' => "0",
':type' => "message",
':sender_name' => $sender_name,
':to_user_name' => $to_user_name,
':delete_received' => 'no',
':delete_sent' => 'no',
);
$sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";
parent::query($sql, $params);
$this->error = "<div class='alert alert-success'>" . _('Your message has been sent.') . "</div>";
Will really appreciate any help..
This is what worked for me, i hope this helps someone else in similar position
while ($value = $stmt->fetch(PDO::FETCH_ASSOC)) {
$params = array(
':to_user_id' => $value['user_id'],
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':sender_id' => $this->user_id,
':status' => "0",
':type' => "message",
':sender_name' => $sender_name,
':to_user_name' => $value['name'],
':delete_received' => 'no',
':delete_sent' => 'no',
);
$sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";
parent::query($sql, $params);
}

SQL Results as PHP Array

I am trying to solve this. Maybe this is absolutly incorrect, I need some help.
I have a table like this:
INSERT INTO `municipios` (`id`, `provincia`, `municipio`) VALUES (1, 1, 'Alegría-Dulantzi'), (2, 1, 'Amurrio'), (3, 1, 'Añana'), (4, 1, 'Aramaio'), (5, 1, 'Armiñón'),
And this is my handler:
$query = "SELECT * FROM municipios WHERE provincia='1'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$groups = array(
$row{'id'} => array(
'name' => $row{'municipio'},
'description' => 'Agrupación electoral de ' . $row{'municipio'},
'status' => 'public',
'enable_forum' => 1,
),
);
}
With this I got just the last result. I have tried another options, but doesn´t work.
Something like this should solve your issue:
$groups[] = array(
$row['id'] => array(
'name' => $row['municipio'],
'description' => 'Agrupación electoral de ' . $row['municipio'],
'status' => 'public',
'enable_forum' => 1,
),
);
However, may i suggest your use PDO. There is a pretty good article http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ that should get you going in the right direction.
Is this an issue with the way you're appending the groups array? I think your overwriting the array every time u set a value. Try using array_push() to append to groups.
Let us know of that helps!
array_push()
http://php.net/manual/en/function.array-push.php

how to add attributes to a category in magento with sql?

How can i add a attribute to a category in magento ?
I have read some tutorials and they said just insert two records but that doesn't work
Already changed a lot and playing with the values but still no result
After each change i have cleared the cache so that is nog the problem
I am working with magento 1.5 and flat tables
inserted the next querys and testing with the values
INSERT INTO `eav_attribute`
(
`attribute_id`,
`entity_type_id`,
`attribute_code`,
`attribute_model`,
`backend_model`,
`backend_type`,
`backend_table`,
`frontend_model`,
`frontend_input`,
`frontend_label`,
`frontend_class`,
`source_model`,
`is_required`,
`is_user_defined`,
`default_value`,
`is_unique`,
`note`
)
VALUES
(
158,
3,
'uitslag',
NULL,
'',
'varchar',
'',
'',
'text',
'Uitslag',
'',
'',
1,
0,
'',
0,
''
);
And
INSERT INTO `eav_entity_attribute`
(
`entity_attribute_id`,
`entity_type_id`,
`attribute_set_id`,
`attribute_group_id`,
`attribute_id`,
`sort_order`
)
VALUES
(
158,
3,
3,
3,
158,
61
);
Anyone a idea ?
Thanks in advance
Try something like this:
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('category', 'uitslag', array(
'label' => 'Uitslag',
'type' => 'varchar',
'input' => 'text',
'visible' => true,
'required' => false,
'position' => 1,
));
The simply add values to categories either through the Magento admin, or directly into the catalog_category_entity_varchar table.

Categories