Answer the question
In order to leave comments, you need to log in
php oracle insert vs update?
Thanks to the user Kerm who helped me fix this code, everything works. But now the question arose of how to check for adding a record, if it exists, then nothing needs to be done, but if not, then add or update the missing records. How to implement such functionality correctly?
I look towards merge, but I do not know how to implement it. Please forgive me for creating a new question because my previous question was drowned.
<blockquote>require_once "simple_html_dom.php";
error_reporting(E_ALL);
ini_set('display_errors', '1');
$conn = oci_connect('ddd', 'ddd', '127.0.0.1/orcl', 'AL32UTF8');
$brands = array();
$html = str_get_html(file_get_contents('https://kolesa.kz/cars/'));
$i = 'INSERT ALL';
$select = $html->find('select[id=auto-car-mm-0]', 0);
foreach($select->find('option') as $opt)
{
if($opt->value == '') continue;
$i .= ' INTO kolesa_brands (id, name) VALUES('.$opt->value.', \''.$opt->plaintext.'\')';
$brands[$opt->value] = $opt->plaintext;
}
$insert = oci_parse($conn, $i.' SELECT * FROM dual;');
oci_execute($insert);</blockquote>
Answer the question
In order to leave comments, you need to log in
You can also try this option
BEGIN
UPDATE kolesa_brands SET name = 'name' WHERE id = <id>;
IF SQL%NOTFOUND THEN
INSERT INTO kolesa_brands (id, name) VALUES (<id>, '<name>');
END IF;
END;
<id>, <name>
, you need to substitute the values of $opt->value and $opt->plaintext). Since merge requires two tables, you can try using the dual virtual tablemerge into kolesa_brands t
using ( select <id> id, '<name>' name from dual ) n on ( t.id = n.id)
when matched then
update set t.name = n.name
when not matched then
insert (t.id, t.name) values (n.id, n.name)
<blockquote>require_once "simple_html_dom.php";
error_reporting(E_ALL);
ini_set('display_errors', '1');
$conn = oci_connect('ddd', 'ddd', '127.0.0.1/orcl', 'AL32UTF8');
$brands = array();
$html = str_get_html(file_get_contents('https://kolesa.kz/cars/'));
$i = 'begin ';
$i .= '\r\n';
$select = $html->find('select[id=auto-car-mm-0]', 0);
foreach($select->find('option') as $opt)
{
if($opt->value == '') continue;
$i .= 'merge into kolesa_brands t ';
$i .= 'using (select '.$opt->value.' id, \''.$opt->plaintext.'\' name from dual) n on (t.id = n.id) ';
$i .= 'when matched then ';
$i .= 'update set t.name = n.name ';
$i .= 'when not matched then ';
$i .= 'insert (t.id, t.name) values (n.id, n.name);';
$i .= '\r\n';
$brands[$opt->value] = $opt->plaintext;
}
$i .= '\r\n';
$i .= 'end;';
$insert = oci_parse($conn, $i);
oci_execute($insert);</blockquote>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question