0

I want to insert data from one table into another where one field equals another in both tables. So far this works. The problem is, I also need to insert additional data into those same rows that is not included on the first table.


//enter rows into database 
foreach($_POST['sku'] as $row=>$sku)
{ 
//this is the data that needs to be added to the table 
$item_sku=$sku;
$image="/$item_sku.jpg";
$small_image="/$item_sku.jpg"; 
$thumbnail="/$item_sku.jpg";

//currently this is what is working to import data from one table to the other
$sql= "INSERT INTO magento_import (sku, description, price) 
       SELECT PR_SKU, PR_Description, PR_UnitPrice
       FROM products 
       WHERE PR_SKU = '$sku'";

//I need something here to add the above variables to the same row where PR_SKU = '$sku'

if (!mysql_query($sql))
{
die('Error: '.mysql_error()); 
}
echo "$row record added";
}

The columns for the missing data on magento_table are called 'image', 'small_image', and 'thumbnail'. This is simple a hack to put data from an old product table into a new product table, export as a CSV, and run a profile in Magento. I don't need to worry about SQL injections. It's something I'm running off of a local machine. I'm trying to avoid as much manual data entry as possible while switching products over to a new ecommerce system. Thanks for any help you can give.

2
  • 1
    Do I get you right: The additional data comes from outside and doesn't reside somewhere in the old database? You probably have to do some interprocessing and split up SQL statements: Fetch data via sql, enrich data by adding values to a php array, store new data array via sql. Commented Oct 22, 2012 at 18:22
  • Please don't use mysql_query in new applications. It's very difficult to use correctly and can lead to hazardously bad code full of SQL injection points. Your example here is a gigantic liability that is asking to be exploited. Using PDO or mysqli is considerably safer, and easier to use correctly. Commented Oct 22, 2012 at 19:26

3 Answers 3

2

Selecting literal values should do what you intend:

$sql= "INSERT INTO magento_import (sku, description, price, image, small_image, thumbnail) 
       SELECT PR_SKU, PR_Description, PR_UnitPrice, \"$image\", \"$small_image\", \"$thumbnail\"
       FROM products 
       WHERE PR_SKU = '$sku'";
Sign up to request clarification or add additional context in comments.

1 Comment

Selecting literal values worked like a charm. Awesome. Thank you very much.
0

You can use another update statement to do this. I doubt if you can do this with one single query

foreach($_POST['sku'] as $row=>$sku)
{ 
//this is the data that needs to be added to the table 
$item_sku=$sku;
$image="/$item_sku.jpg";
$small_image="/$item_sku.jpg"; 
$thumbnail="/$item_sku.jpg";

//currently this is what is working to import data from one table to the other
$sql= "INSERT INTO magento_import (sku, description, price) 
SELECT PR_SKU, PR_Description, PR_UnitPrice
FROM products 
WHERE PR_SKU = '$sku'";

//I need something here to add the above variables to the same row where PR_SKU = '$sku'

if (!mysql_query($sql))
{
die('Error: '.mysql_error()); 
}else {

$sql = "UPDATE magento_import SET item='$item',image='$image',small_image='$small_image',thumbnail='$thumbnail' WHERE PR_SKU = '$sku'";
echo "$row record added";
}

}

Comments

0

You can just add the variables directly to the query. As long as they are quoted they will work as simple values.

$sql= "INSERT INTO magento_import (sku, description, price, x, y, z) 
   SELECT PR_SKU, PR_Description, PR_UnitPrice, '$x' AS x, '$y' AS y, '$z' AS z
   FROM products 
   WHERE PR_SKU = '$sku'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.