Insert into table search_index in Prestashop fails - php

I have absolutely no idea whats going on.
I'm trying INSERT into table ps_search_index like this:
$sql = 'INSERT INTO ps_search_index (id_product,id_word,weight)
VALUES ('.$id_product.','.$getID.',9)';
Db::getInstance()->Execute($sql);
and it doesn't work if there is $id_product.
But its working when i write eg. 1234. id_product is of course INT and any other value in this place working.
Of course no other errors.
Whats should i do?

Try this:
Db::getInstance()->execute('
INSERT INTO '._DB_PREFIX_.'search_index (id_product, id_word, weight)
VALUES ('.(int)$id_product.', '.(int)$getID.', 9)
ON DUPLICATE KEY UPDATE weight = weight + VALUES(weight)', false
);
I always recommend to cast the var. In this case you have to add ON DUPLICATE..., like is do in the Search class.

I've done this using tutorial:
http://blog.belvg.com/developer-tips-how-prestashop-search-works.html

In ps_search_index database table of PrestaShop, the combination of id_product and id_word is the primary key of the table.
Since the primary key of any database table has to be unique, your query is failing as the combination that you are adding in your query must already be existing in the table. In order to fix this, you can apply a check before inserting any new row.

Related

How to duplicate a row in the same table in prestashop 1.6

I know I am supposed to create a temporary table based on an existing row, remove the id and then insert an new row in my "real" table based on the temporary table. But it does not work in my prestashop.
$id = 4;
$createTemp = Db::getInstance()->Execute('CREATE TABLE temp_table AS SELECT * FROM my_table WHERE id="'.$id.'"');
$updateTemp = Db::getInstance()->Execute('UPDATE temp_table SET id=NULL');
$insertQuery = Db::getInstance()->Execute('INSERT INTO my_table SELECT * FROM temp_table');
$deleteTemp = Db::getInstance()->Execute('DROP TABLE temp_table');
If I make a var_dump of those I always get a FALSE. I tried to make the select only in a query and it does work so my SELECT * FROM my_table WHERE id="'.$id.'" is correct.
I need to be able to duplicate a row and I could'nt find another/a better way to do so. What do I have to change in my code to make it work ?
If source data is OK your code should work,
I would enable Prestashop mode_dev so you can see the query errors on-screen for better debug.
Since you're working with Prestashop - maybe your "my_table" id is not called id but something different like "id_product" ?
If this is the case, first create will fail and all other queries will fail consequently.

PHP MySQL Insert Query that won't insert if already exists

I've been doing some research and haven't found anything that I've been able to make work, unfortunately, and I think that stems from not understanding the MySQL construct in the examples I've been looking at.
What I'm trying to do is run an insert query, and do a check on values in 3 specific columns to ensure they don't exist, then insert, else do nothing.
My Table is pretty basic: id(int11), user(varchar(45)), label(varchar(255)), agent(varchar(255)), empid(varchar(10)).
My id is my Primary, with Auto increment, and here is my code I currently have that works on inserting, but doesn't have the handling in place for duplicates:
$i = 0;
foreach ($agents as $ag){
$sql = mysql_query("INSERT INTO `pms_users`
(`id`,`user`,`label`,`agent`,`empid`)
VALUES
(NULL, '$user','$group','$labels[$i]','$ag')");
$i ++;
}
The three columns I need to check against are the $user, $group, and $ag.
Add a unique index for (user, label, empid). Then the database won't allow you to create duplicates.
ALTER TABLE pms_users
ADD UNIQUE INDEX (user, label, empid);
If you can only have one row per combination of user, label and agent, you should define them as a unique constraint:
ALTER TABLE pms_users ADD CONSTRAINT pms_users_unq UNIQUE (`user`, `label`, `agent`);
And then let the database do the heavy lifting with an insert-ignore statement:
INSERT IGNORE INTO `pms_users`
(`user`, `label`, `agent`, `empid`)
VALUES ('some_user', 'some_label', 'some_agent', 123)
You can try insert on duplicate key update query.. It checks duplicate keys. If they exist MySQL do update query if not exist MySQL doing insert query.
Sure in your database you should declare unique keys.
Here is MySQL documentation for this case
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

Check with sql if two tables are unique PHP

How can I check in MYSQL PHP if two columns are unique then not insert again, else if just one column is unique then insert, is that even possible to do in php?
EDIT:
Lets say I have a table like this,
userId | codeId
And I I send a query like this,
$query = $pdo->prepare('insert into table (userId, codeId) values (?,?)');
So now I want to check if userId and codeId are added already once do not insert again, and if just one is added, then do insert the entire query,
I hope its more understanding.
Set up a unique key for those columns, then the mysql query will FAIL when you try to insert.
Use REPLACE INTO instead of INSERT INTO ... ?
Do something like the code below (where TEXT_ID and TEXT_CATEGORY, are keys of table):
INSERT INTO
table_texts
SET
text_id = 174,
text_category = "pam_texto"
ON DUPLICATE KEY UPDATE
text_id = 174,
text_category = "pam_texto";
The above code tries to insert, but if the keys are duplicated performs an update on the line.

Database normalization and lazy development

I believe the question have emerged as my irritation of doing twice as much work as I could imagine is necessary.
I accept the idea that I could be lacking experience with both MySQL and PHP to think of a simpler solution.
My issue is that I have several tables (and I'd might be adding more) and of these is a parent table, only containing two fields - an id (int) and a name identifying it.
At this moment, I have seven tables with at least 15 fields in each one. Every table has a field, containing the id which I can link to the parent table.
All of these data isn't required to be filled - you will just have to create that one entry in the parent table. For the other tables, I have separate forms.
Now, these forms are made for updating the data in the fields, which means I have to pull out the data from the table if any data is available.
What I would like to do is when I receive the data from my form, I could just use an UPDATE query in my model. But if the table I want to update doesn't have an entry for that specific id, I need to do an insert.
So, my current pseudo code is like this:
$sql = "SELECT id FROM table_x WHERE parent_id = ".$parent_id;
$res = $mysql_query($sql);
if( mysql_num_rows($res) == 1 )
{
$sql = "UPDATE table_x SET ... WHERE parent_id = ".$parent_id;
}
else
{
$sql = "INSERT INTO table_x VALUES ( ... )";
}
mysql_query($sql);
I have two do this for every table I have - can I do something different or smarter or is this just the way it has to be done? Cause this seems very inefficient to me.
Use
INSERT ... ON DUPLICATE KEY UPDATE Syntax
It will insert if record not found,
otherwise, it will update existing record,
and you can skip the check before insert - details
This assuming relation for each 7 table to the parent table is 1:1
Or use REPLACE instead of INSERT - it's an insert, but will do an DELETE and then INSERT when a unique key (such as the primary key) is violated.
in mysql you can do this:
INSERT INTO table
(
col1,
col2
) VALUES(
'val1',
'val2'
) ON DUPLICATE KEY UPDATE table SET
col2 = 'val2'
take a look at the documentation for more information
mysql_query("UPDATE table table_x ..... WHERE parent_id=".$parent_id);
if (mysql_affected_rows()==0) {
mysql_query("INSERT INTO .....");
}

PHP, MySQL: Duplicate series of rows, but change 1 column?

I have a table called scheduler_sched which has several columns, including a column called schedule_id.
I need a function where I can pass 2 ids (copy_from_id, copy_to_id) as parameters. And what I need to do is take every row where schedule_id = copy_from_id AND duplicate it but change the copy_from_id to the copy_to_id
So basically I want to to the equivalient of this:
UPDATE scheduler_sched SET schedule_id = 32 WHERE schedule_id = 28
Only I do not want to UPDATE any rows, I want to create duplicates with the new ID's
Does this make sense?
How can I do this?
THANKS!
(By the way schedule_id is not a unique/index field on this table)
Insert into scheduler_sched (column1, column2, column3,schedule_id )
Select column1, column2, column3, 32 from scheduler_sched WHERE schedule_id = 28
I think that ON DUPLICATE KEY UPDATE syntax may help you:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
e.g.:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Just INSERT a new row instead of updating. SELECT first if that schedule_id 28 exists, and if it does, insert a new one with that being the ID.
Since you haven't specified a version of MySQL, I'm going to assume that it is the lastest (5.4).
Assuming I am understanding you correctly, you should be able to implement this using triggers: http://dev.mysql.com/doc/refman/5.4/en/create-trigger.html
One of the benefits of using triggers, is it is all handled by the database itself.

Categories