1

I must update table cscart_products by csv file output.csv.

I have created a temporary table (your_temp_table) and with command INNER JOIN updated cscart_products table (product_code, amount).

When executing the following code, MySQL report this error:

The user update failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE cscart_products INNER JOIN your_temp_table on your_temp_table.amount = cs' at line 6

Is there an error? Is the SQL syntax wrong?

<?php
$sql = "LOAD DATA INFILE 'output.csv'
       INTO TABLE your_temp_table
       FIELDS TERMINATED BY ','
    (product_code, amount);

UPDATE cscart_products
INNER JOIN your_temp_table on your_temp_table.amount = cscart_products.amount
SET cscart_products.product_code = your_temp_table.product_code;";

$con=mysqli_connect("localhost","xxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_error()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
};

$result = mysqli_query($con, $sql);

if (mysqli_affected_rows($con) == 1) {
  $message = "The data was successfully added!";
} else {
  $message = "The user update failed: ";
  $message .= mysqli_error($con); 
};

echo $message;
mysqli_close($con);
?>
0

1 Answer 1

3

in mysqli they should be executed as two queries

$sql = "LOAD DATA INFILE 'output.csv'
       INTO TABLE your_temp_table
       FIELDS TERMINATED BY ','
    (product_code, amount)";

$sql2 = "UPDATE cscart_products
INNER JOIN your_temp_table on your_temp_table.amount = cscart_products.amount
SET cscart_products.product_code = your_temp_table.product_code";


$result = mysqli_query($con, $sql);
$result = mysqli_query($con, $sql2); 

Or, you have to use multi_query

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.