To insert or update data if data exists, I was using ‘REPLACE INTO…’ in a var $query_string
like this :
dbDelta( $query_string );
For some reason, the row is not insert. Why ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The reason is due to this code inside dbDelta() :
// Create a tablename index for an array ($cqueries) of queries.
foreach ( $queries as $qry ) {
if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
$cqueries[ trim( $matches[1], '`' ) ] = $qry;
$for_update[ $matches[1] ] = 'Created table ' . $matches[1];
} elseif ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) {
array_unshift( $cqueries, $qry );
} elseif ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) {
$iqueries[] = $qry;
} elseif ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) {
$iqueries[] = $qry;
} else {
// Unrecognized query type.
}
}
ths function dbDelta() does not support ‘REPLACE INTO’ and that calls in the else with // Unrecognized query type. comment.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0