Jump to content

Recommended Posts

I am trying to update the prices on one shopping cart using the prices from another shopping carts database on another server. I am still a newbie at PHP and have been working on this all day. If anyone could help me out I would really appreciate it.

 

<?php 
// CSCart DB Info
$hostcs = "localhost";
$usercs = "user";
$passcs = "pass";
$dbnamecs = "cscart0";
// OSC DB Info
$hostosc = "192.168.6.2";
$userosc = "user";
$passosc = "pass";
$dbnameosc = "shop0";

// Connect to cscart
$handlecscart = mysql_connect($hostcs, $usercs, $passcs);

// Connect to osc
$handleosc = mysql_connect($hostosc, $userosc, $passosc);

// Select the cscart database
mysql_select_db($dbnamecs,$handlecscart)
or die("Couldn't connect to DB");

// Select the osc database
mysql_select_db($dbnameosc,$handleosc)
or die("Couldn't connect to DB");

// Query CSCART
$selectcs = "SELECT product_code, price FROM cscart_products, cscart_product_prices WHERE cscart_products.product_id = cscart_product_prices.product_id";
if (!$selectcs) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}

// Query OSC
$selectosc = "SELECT products_model, products_price FROM products WHERE products_id = products_id";
if (!$selectosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}

// CSCART results
$resultcs = mysql_query($selectcs,$handlecscart); 
if (!$resultcs) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}

// OSC Results
$resultosc = mysql_query($selectosc,$handleosc);
if (!$resultosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}
while ( $rowcs = mysql_fetch_array($resultcs) AND $rowosc = mysql_fetch_array($resultosc) ) {
$cspart = $rowcs['product_code'] ;
$csprice = $rowcs['price'];
$oscpart = $rowosc['products_model'];
$oscprice = $rowosc['products_price'];
echo "<p>Part $cspart is being updated to $oscprice" . "<br />";
$result = mysql_query("UPDATE cscart_product_prices SET price='12.00' WHERE cscart_products.product_code = $cspart AND cscart_products.product_id = cscart_product_prices.product_id",$handlecscart) 
or die(mysql_error());


}
?>

Link to comment
https://forums.phpfreaks.com/topic/65331-newbie-needs-help-please/
Share on other sites

i think the problem is the logic..

 

do you have a UniqueID for each record ?

 

i'll take a guess and assume its product_code

 

ok the logic should be something like this.

First a word of warning it 4am here and i am about to go to sleep so please backup your database first and read the comments and make sense of the logic

 

<?php 
// CSCart DB Info
$hostcs = "localhost";
$usercs = "user";
$passcs = "pass";
$dbnamecs = "cscart0";
// OSC DB Info
$hostosc = "192.168.6.2";
$userosc = "user";
$passosc = "pass";
$dbnameosc = "shop0";

// Connect to cscart
$handlecscart = mysql_connect($hostcs, $usercs, $passcs);

// Connect to osc
$handleosc = mysql_connect($hostosc, $userosc, $passosc);

// Select the cscart database
mysql_select_db($dbnamecs,$handlecscart)
or die("Couldn't connect to DB");

// Select the osc database
mysql_select_db($dbnameosc,$handleosc)
or die("Couldn't connect to DB");


// Query OSC
$selectosc = "SELECT products_model, products_price FROM products ";
if (!$selectosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}

//****DON'T NEED TO PULL DATE FROM HERE YET*****
// Query CSCART
//$selectcs = "SELECT product_code, price, product_id FROM cscart_products, cscart_product_prices";
// if (!$selectcs) {
//   die('<p>Error performing query: ' . mysql_error() . '</p>');
// }
// CSCART results
//$resultcs = mysql_query($selectcs,$handlecscart); 
// if (!$resultcs) {
//   die('<p>Error performing query: ' . mysql_error() . '</p>');
// }
//********************************

// OSC Results
$resultosc = mysql_query($selectosc,$handleosc);
if (!$resultosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}
//*******ONLY LOOP ONE
//while ( $rowcs = mysql_fetch_array($resultcs) AND $rowosc = mysql_fetch_array($resultosc) )
while ( $rowcs = mysql_fetch_array($resultcs) )
{
$cspart = $rowcs['product_code'] ;
$csprice = $rowcs['price'];


$csPID = $rowcs['product_id'] ;
//Find the New Product price etc
$selectosc = "SELECT products_model, products_price FROM products WHERE products_id = $csPID";
$resultcs = mysql_query($selectcs,$handlecscart); 
$rowosc = mysql_fetch_array($resultosc)
$oscpart = $rowosc['products_model'];
$oscprice = $rowosc['products_price'];


echo "<p>Part $cspart is being updated to $oscprice" . "<br />";
$result = mysql_query("UPDATE cscart_product_prices SET price='$oscprice'
	WHERE cscart_products.product_id = $csPID",$handlecscart) 
	or die(mysql_error());
}
?>

Thanks man. There was one ";" missing and I added that in but now I am getting:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/coastalmicrosupply.com/httpdocs/migrate_new.php on line 62

 

I went ahead and added the code with the additional ";" here for reference.

 

 

<?php 
// CSCart DB Info
$hostcs = "localhost";
$usercs = "user";
$passcs = "pass";
$dbnamecs = "cscart0";
// OSC DB Info
$hostosc = "192.168.6.2";
$userosc = "user";
$passosc = "pass";
$dbnameosc = "shop0";

// Connect to cscart
$handlecscart = mysql_connect($hostcs, $usercs, $passcs);

// Connect to osc
$handleosc = mysql_connect($hostosc, $userosc, $passosc);

// Select the cscart database
mysql_select_db($dbnamecs,$handlecscart)
or die("Couldn't connect to DB");

// Select the osc database
mysql_select_db($dbnameosc,$handleosc)
or die("Couldn't connect to DB");


// Query OSC
$selectosc = "SELECT products_model, products_price FROM products ";
if (!$selectosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}

//****DON'T NEED TO PULL DATE FROM HERE YET*****
// Query CSCART
//$selectcs = "SELECT product_code, price, product_id FROM cscart_products, cscart_product_prices";
// if (!$selectcs) {
//   die('<p>Error performing query: ' . mysql_error() . '</p>');
// }
// CSCART results
//$resultcs = mysql_query($selectcs,$handlecscart); 
// if (!$resultcs) {
//   die('<p>Error performing query: ' . mysql_error() . '</p>');
// }
//********************************

// OSC Results
$resultosc = mysql_query($selectosc,$handleosc);
if (!$resultosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}
//*******ONLY LOOP ONE
//while ( $rowcs = mysql_fetch_array($resultcs) AND $rowosc = mysql_fetch_array($resultosc) )
while ( $rowcs = mysql_fetch_array($resultcs) )
{
$cspart = $rowcs['product_code'] ;
$csprice = $rowcs['price'];


$csPID = $rowcs['product_id'] ;
//Find the New Product price etc
$selectosc = "SELECT products_model, products_price FROM products WHERE products_id = $csPID";
$resultcs = mysql_query($selectcs,$handlecscart); 
$rowosc = mysql_fetch_array($resultosc);
$oscpart = $rowosc['products_model'];
$oscprice = $rowosc['products_price'];


echo "<p>Part $cspart is being updated to $oscprice" . "<br />";
$result = mysql_query("UPDATE cscart_product_prices SET price='$oscprice'
	WHERE cscart_products.product_id = $csPID",$handlecscart) 
	or die(mysql_error());
}
?>

full update

<?php 
// CSCart DB Info
$hostcs = "localhost";
$usercs = "user";
$passcs = "pass";
$dbnamecs = "cscart0";
// OSC DB Info
$hostosc = "192.168.6.2";
$userosc = "user";
$passosc = "pass";
$dbnameosc = "shop0";

// Connect to cscart
$handlecscart = mysql_connect($hostcs, $usercs, $passcs);

// Connect to osc
$handleosc = mysql_connect($hostosc, $userosc, $passosc);

// Select the cscart database
mysql_select_db($dbnamecs,$handlecscart)
or die("Couldn't connect to DB");

// Select the osc database
mysql_select_db($dbnameosc,$handleosc)
or die("Couldn't connect to DB");

// Query OSC
$selectosc = "SELECT products_model, products_price FROM products ";
if (!$selectosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}

// OSC Results
$resultosc = mysql_query($selectosc,$handleosc);
if (!$resultosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}
//*******ONLY LOOP ONE
//while ( $rowcs = mysql_fetch_array($resultcs) AND $rowosc = mysql_fetch_array($resultosc) )
while ( $rowosc = mysql_fetch_array($resultosc) )
{
$oscpart = $rowosc['product_code'] ;
$oscprice = $rowosc['price'];

$oscPID = $rowosc['product_id'] ;
//Find the New Product price etc
$selectosc = "SELECT products_model, products_price FROM products WHERE products_id = $oscPID";
$resultcs = mysql_query($selectcs,$handlecscart); 
$rowcs = mysql_fetch_array($resultcs);
$cspart = $rowcs['products_model'];
$csprice = $rowcs['products_price'];


echo "<p>Part $cspart is being updated to $oscprice" . "<br />";
$result = mysql_query("UPDATE cscart_product_prices SET price='$csprice'
	WHERE cscart_products.product_id = $oscPID",$handlecscart) 
	or die(mysql_error());
}
?>

I was still getting an error and I noticed a few things so I made some changes. Here is the location: http://coastalmicrosupply.com/migrate_new.php

I am still getting the same original issue. Here are the changes.

 

<?php
// CSCart DB Info
$hostcs = "localhost";
$usercs = "user";
$passcs = "pass";
$dbnamecs = "cscart0";
// OSC DB Info
$hostosc = "192.168.6.2";
$userosc = "user";
$passosc = "pass";
$dbnameosc = "shop0";

// Connect to cscart
$handlecscart = mysql_connect($hostcs, $usercs, $passcs);

// Connect to osc
$handleosc = mysql_connect($hostosc, $userosc, $passosc);

// Select the cscart database
mysql_select_db($dbnamecs,$handlecscart)
or die("Couldn't connect to DB");

// Select the osc database
mysql_select_db($dbnameosc,$handleosc)
or die("Couldn't connect to DB");

// Query OSC
$selectosc = "SELECT products_model, products_price FROM products ";
if (!$selectosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}

// OSC Results
$resultosc = mysql_query($selectosc,$handleosc);
if (!$resultosc) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}
//*******ONLY LOOP ONE
//while ( $rowcs = mysql_fetch_array($resultcs) AND $rowosc = mysql_fetch_array($resultosc) )
while ( $rowosc = mysql_fetch_array($resultosc) )
{
// $oscpart = $rowosc['product_code'] ;
$oscpart = $rowosc['products_model'];
// $oscprice = $rowosc['price'];
$oscprice = $rowosc['products_price'];

// $oscPID = $rowosc['product_id'] ;
// Updates should be based on the product model

//Find the New Product price etc
$selectosc = "SELECT products_model, products_price FROM products WHERE products_model = '$oscpart'";
$resultcs = mysql_query($selectosc,$handleosc); 
$rowcs = mysql_fetch_array($resultcs);
$cspart = $rowcs['products_model'];
$csprice = $rowcs['products_price'];


echo "<p>Part $cspart is being updated to $csprice" . "<br />";
// Made some changes so that new price is updated based on $oscpart
$result = mysql_query("UPDATE cscart_product_prices SET price='$csprice'
	WHERE cscart_products.product_id = cscart_products_price.product_id AND cscart_products.product_code = '$oscpart'",$handlecscart) 
	or die(mysql_error());
}
?>

well this is my update i changed a few thing so it makes more sense to me but i don't know thw databse setup etc, the logic is the main thing

 

<?php
// From DB
// CSCart DB Info
$hostF = "localhost";
$userF = "user";
$passF = "pass";
$dbnameF = "cscart0";

//To DB
// OSC DB Info
$hostT = "192.168.6.2";
$userT = "user";
$passt = "pass";
$dbnameT = "shop0";

// Connect to cscart
$handleFrom = mysql_connect($hostF, $userF, $passF);

// Connect to osc
$handleTo = mysql_connect($hostT, $userT, $passT);

// Select the cscart database
mysql_select_db($dbnameF,$handleFrom)
or die("Couldn't connect to DB");

// Select the osc database
mysql_select_db($dbnameT,$handleTo)
or die("Couldn't connect to DB");

// Query OSC
$SqlFrom = "SELECT products_model, products_price FROM products ";

// OSC Results
$resultFrom = mysql_query($SqlFrom, $handleF);
if (!$resultFrom) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}


while ( $FromRow = mysql_fetch_array($resultFrom) )
{
$FromID = $FromRow['product_id'];
$FromPart = $FromRow['products_model'];
$FromPrice = $FromRow['products_price'];

$SqlTo = "SELECT products_model, products_price 
	FROM products 
	WHERE product_id = '$FromID' 
	AND product_code = '$FromPart'";

$resultTo = mysql_query($SqlTo,$handleT); 
$ToRow = mysql_fetch_array($resultTo);
$ToPart = $ToRow['products_model'];
$ToPrice = $ToRow['products_price'];

echo "<p>Part $FromPart is being updated from $FromPrice to $ToPrice" . "<br />";

$SqlUpdate = "UPDATE cscart_product_prices 
	SET price='$ToPrice'
	WHERE product_id = '$FromID'  
	AND product_code = '$FromPart'"
$result = mysql_query($SqlUpdate, $handlecscart) 
	or die(mysql_error());
}
?>

Based on that I made some changes for my database and now it tells me that a table does not exist when it does. As far as I can tell the query looks fine.

 

Part TH8BG-YF is being updated from 665.0000 to

Unknown table 'cscart_products' in where clause

 

<?php
// From DB
// OSC DB Info
$hostF = "192.168.6.2";
$userF = "user";
$passF = "pass";
$dbnameF = "shop0";

//To DB
// CSCART DB Info
$hostT = "localhost";
$userT = "user";
$passT = "pass";
$dbnameT = "cscart0";

// Connect to osc
$handleFrom = mysql_connect($hostF, $userF, $passF);

// Connect to cscart
$handleTo = mysql_connect($hostT, $userT, $passT);

// Select the osc database
mysql_select_db($dbnameF,$handleFrom)
or die("Couldn't connect to DB");

// Select the cscart database
mysql_select_db($dbnameT,$handleTo)
or die("Couldn't connect to DB");

// Query OSC
$SqlFrom = "SELECT products_model, products_price FROM products ";

// OSC Results
$resultFrom = mysql_query($SqlFrom, $handleFrom);
if (!$resultFrom) {
   die('<p>Error performing query: ' . mysql_error() . '</p>');
}


while ( $FromRow = mysql_fetch_array($resultFrom) )
{
$FromID = $FromRow['product_id'];
$FromPart = $FromRow['products_model'];
$FromPrice = $FromRow['products_price'];

$SqlTo = "SELECT product_code, price
	FROM cscart_products, cscart_product_prices 
	WHERE product_code = '$FromPart'";

$resultTo = mysql_query($SqlTo,$handleTo)
		or die(mysql_error());
$ToRow = mysql_fetch_array($resultTo);
$ToPart = $ToRow['products_model'];
$ToPrice = $ToRow['products_price'];

echo "<p>Part $FromPart is being updated from $FromPrice to $ToPrice" . "<br />";

$SqlUpdate = 
"UPDATE cscart_product_prices SET price='$ToPrice' 
WHERE cscart_products.product_id = cscart_product_prices.product_id 
AND cscart_products.product_code = '$FromPart'";
$result = mysql_query($SqlUpdate, $handleTo) 
	or die(mysql_error());
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.