I need to insert when rows don't exist in the tickets table and/or update when rows do exist with 1 query.
I tried the following and it doesn't add new records and it doesn't up the existing records.
$query_upsert = mysqli_query($mysqli,
"INSERT INTO tickets (
ticket_companyname,
ticket_ordernumber,
ticket_datetimedeliverydate,
ticket_ritech,
ticket_ticketstatus
)
SELECT
nextgenorder_companyname,
nextgenorder_ordernumber,
nextgenorder_deliverydate,
'$ticket_ritech',
'$ticket_ticketstatus'
FROM nextgenorders2
WHERE
nextgenorder_companyname LIKE CONCAT(SUBSTRING_INDEX('$nextgenorder_companyname', ' ', 1),'%')
AND nextgenorder_deliverydate='$nextgenorder_deliverydate'
ON DUPLICATE KEY UPDATE,
ticket_companyname='$ticket_companyname',
ticket_ordernumber='$ticket_ordernumber',
ticket_datetimedeliverydate='$ticket_datetimedeliverydate',
ticket_ritech='$ticket_ritech',
ticket_ticketstatus='$ticket_ticketstatus'
WHERE
ticket_companyname LIKE CONCAT(SUBSTRING_INDEX('$nextgenorder_companyname', ' ', 1),'%')
AND ticket_datetimedeliverydate='$nextgenorder_deliverydate'
)"
);
Can you use WHERE clause in a upsert query? I have all errors on and i'm not getting any errors.
Please help.
Thanks,
mysqliyou should be using parameterized queries andbind_paramto add any data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put$_POST,$_GETor data of any kind directly into a query, it can be very harmful if someone seeks to exploit your mistake."SELECT this should not work"and if you don't get an error you have to fix that first.WHEREclause at the end of the query, afterON DUPLICATE KEY UPDATE .... It's not valid syntax.ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE ticket_companyname LIKE CONCAT(SUBSTRING_INDEX('$nextgenorder_com' at line 58SELECTwork in isolation?