How to update a mysql table?

I’m not sure about how to go about this?

I have a database that was created from a data feed txt file. I want to add more fields to the table. When I update the table with the next text file I don’t want to delete everything. I just want to update the table. For example, if a product changes price, ad copy, status etc. I want it to update those fields. If a product is no longer available it deletes the whole product row. If there is a new product it adds a new row. But I don’t want it to mess up the columns I’ve added. Know what I mean?

The way its set up now it will delete the whole table and recreate a new table.

Basically all I want to do is update the table with anything new and not to hurt the old table.

How would I go about doing this? I can’t find an example online.

Thanks

Do you have a show create table?

check the mysql manual, there’s a nice example in there illustrating how to use the INSERT statement with ON DUPLICATE KEY UPDATE option

deletions, however, are going to require special handling

I’m not sure what you mean. I don’t know what “show create table” is.

How was your table created? I.o.w. How does your table looks like (what fields with what datatypes). If you run show create table your_table it should return the setup of your table. Copy and paste it here!

SHOW CREATE TABLE is a mysql command…

SHOW CREATE TABLE [I]tablename[/I]

this produces the CREATE TABLE statement for the table, which allows us to see what it looks like, the column names, indexes defined, etc.

I always learn new stuff here!:slight_smile:

Here is show create table


CREATE TABLE `naturehills` (
 `ProductID` int(11) NOT NULL default '0',
 `Name` varchar(255) collate latin1_general_ci NOT NULL default '',
 `MerchantID` varchar(50) collate latin1_general_ci NOT NULL default '',
 `Merchant` varchar(50) collate latin1_general_ci NOT NULL default '',
 `Link` text collate latin1_general_ci NOT NULL,
 `Thumbnail` text collate latin1_general_ci NOT NULL,
 `BigImage` text collate latin1_general_ci NOT NULL,
 `Price` varchar(50) collate latin1_general_ci NOT NULL default '',
 `RetailPrice` varchar(50) collate latin1_general_ci NOT NULL default '',
 `Category` varchar(50) collate latin1_general_ci NOT NULL default '',
 `SubCategory` varchar(50) collate latin1_general_ci NOT NULL default '',
 `Description` longtext collate latin1_general_ci NOT NULL,
 `Custom1` text collate latin1_general_ci NOT NULL,
 `Custom2` text collate latin1_general_ci NOT NULL,
 `Custom3` text collate latin1_general_ci NOT NULL,
 `Custom4` text collate latin1_general_ci NOT NULL,
 `Custom5` text collate latin1_general_ci NOT NULL,
 `LastUpdated` varchar(100) collate latin1_general_ci NOT NULL default '',
 `status` varchar(50) collate latin1_general_ci NOT NULL default '',
 PRIMARY KEY  (`ProductID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

You should be able to update required fields using the ProductID


UPDATE
   naturehills
SET
  Price = YOUR-NEW-VALUE
WHERE
  ProductID IN ( 1, 3, 5, 6, 8, 9, etc )    

Thank You

By the way…

UPDATE
   naturehills
SET
  Price = YOUR-NEW-VALUE
WHERE
  ProductID IN ( 1, 3, 5, 6, 8, 9, etc )

Will this add whole new rows if there are new products?

No for new products you need an insert statement. This is to update the table, in this instance the field price for the records in the where statement

see also post #3 :slight_smile:

Here’s what I’ve managed to do. It works but it doesn’t update individual field cells. It will insert new rolls, but if I change something like the price or description it doesn’t update to the new price etc.

I was wondering if I need to create another column for a counter column?

INSERT INTO $maintable (ProductID, Name, MerchantID, Merchant, Link, Thumbnail, BigImage, Price, RetailPrice, Category, SubCategory, Description, Custom1, Custom2, Custom3, LastUpdated, status) 
VALUES ({$ProductID}, {$Name}, {$MerchantID}, {$Link}, {$Thumbnail}, {$BigImage}, {$Price} {$RetailPrice}, {$Category}, {$title}, {$SubCategory}, {$Description}, {$Custom1}, {$Custom2}, {$Custom3}, {$LastUpdated}, {$status}) 
ON DUPLICATE KEY UPDATE ProductID={$ProductID}, Name={$Name}, MerchantID={$MerchantID}, Merchant={$Merchant}, Link={$Link}, Thumbnail={$Thumbnail}, BigImage={$BigImage}, RetailPrice={$RetailPrice}, Category={$Category}, SubCategory={$SubCategory}, Description={$Description}, Custom1={$Custom1}, Custom2={$Custom2}, Custom3={$Custom3}, LastUpdate={$LastUpdate}, status={$status}; 

could you please do an echo on that sql statement just prior to execution

I’m not sure how to do an “echo on that sql statement just prior to execution.” I couldn’t find much on the net about it, but I did find this:

echo "
SQL Statement = $sql <br>";

I ran it like this:

$sql = mysql_query(“INSERT INTO $maintable (Prod…
echo "
SQL Statement = $sql <br>”;

Above and below the statement. I might be way off because the results both ways were:

SQL Statement =
SQL Statement =
SQL Statement =
SQL Statement =
SQL Statement =
SQL Statement =
SQL Statement =
SQL Statement =
SQL Statement =
SQL Statement = …

you’re echoing the resource that was assigned to the $sql variable

try like this –


$sqlstring = "INSERT INTO $maintable (Prod... 
echo "\
SQL Statement = $sqlstring <br>";
$sql = mysql_query($sqlstring)

and i don’t even do php :smiley:

Maybe it will help if I show you the complete code. Here’s how I have the code. I keep getting the same results with the echo. I’ve tried different locations for the echo.

<?php
$ftp_server = "datafeeds.shareasale.com"; // Do not change this 
$ftp_user_name = "name"; // Replace user_name with your SAS login name
$ftp_user_pass = "pass"; // Replace user_pass with your SAS password
 
$source_file = "/4742/4742.txt"; // Replace both number with the merchant ID 
$destination_file = "4742.txt";
$sasid = "anumber"; // REPLACE sas_id with your SAS ID; Can be found at the top of any SAS page

$maintable = "naturehills";

$to_error = "email@hotmail.com";
$subj_error = "ERROR In Naturehills Feed";
$mssg_error = "Database ERROR: ". mysql_error()."\
\
";
$hdrs_error = "From: [email]email@hotmail.com[/email]\
";

$conn_id = ftp_connect($ftp_server); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

if ((!$conn_id) || (!$login_result)) { 
     echo "<span style=\\"color:red\\"><center>ERROR: FTP connection has failed to $ftp_server as $ftp_user_name.</center></span>";
     exit; 
} else {
     echo "<span style=\\"color:green\\"><center><br /><br />SUCCESS: $ftp_user_name connected to $ftp_server successfully.</center></span>";
}

$download = ftp_get($conn_id, $destination_file, $source_file, FTP_ASCII); 

if (!$download) { 
     echo "FTP download has failed!";
} else {
     echo "<span style=\\"color:green\\"><center>SUCCESS: Downloaded $source_file from $ftp_server as $destination_file</center></span>";
}
ftp_close($conn_id);
 
/* Include Configuration File */
  require_once("config.php");

$FeedFile = $destination_file;

$feed = fopen($FeedFile, 'r');
$rowNum = 0;

while($data = fgetcsv($feed, 50000, "|")){

   $ProductID = addslashes($data[0]);
   $Name = $data[1];
   $Name = preg_replace("/[^a-zA-Z0-9s]/", " ", $Name);
   $Name = preg_replace("/\\s+/", " ", $Name);
   $Name = ucwords(strtolower($Name));
   $MerchantID = addslashes($data[2]);
   $Merchant = addslashes($data[3]);
   $Link = addslashes($data[4]);
   $Thumbnail = addslashes($data[5]);
   $BigImage = addslashes($data[6]);
   $Price = addslashes($data[7]);
   $RetailPrice = addslashes($data[8]);
   $Category = addslashes($data[9]);
   $SubCategory = addslashes($data[10]);
   $Description = addslashes($data[11]);
   $Custom1 = addslashes($data[12]);
   $Custom2 = addslashes($data[13]);
   $Custom3 = addslashes($data[14]);
   $LastUpdated = addslashes($data[17]);
   $status = addslashes($data[18]); 
	 
	 
$sql = mysql_query("INSERT INTO $maintable (ProductID, Name, MerchantID, Merchant, Link, Thumbnail, BigImage, Price, RetailPrice, Category, SubCategory, Description, Custom1, Custom2, Custom3, LastUpdated, status) VALUES ({$ProductID}, {$Name}, {$MerchantID}, {$Link}, {$Thumbnail}, {$BigImage}, {$Price} {$RetailPrice}, {$Category}, {$title}, {$SubCategory}, {$Description}, {$Custom1}, {$Custom2}, {$Custom3}, {$LastUpdated}, {$status}) ON DUPLICATE KEY UPDATE ProductID={$ProductID}, Name={$Name}, MerchantID={$MerchantID}, Merchant={$Merchant}, Link={$Link}, Thumbnail={$Thumbnail}, BigImage={$BigImage}, RetailPrice={$RetailPrice}, Category={$Category}, SubCategory={$SubCategory}, Description={$Description}, Custom1={$Custom1}, Custom2={$Custom2}, Custom3={$Custom3}, LastUpdate={$LastUpdate}, status={$status}"); 
echo "\
SQL Statement = $sqlstring <br>";
$sql = mysql_query($sqlstring);

	$rowNum++;
}
echo "\
SQL Statement = $sqlstring <br>";
$sql = mysql_query($sqlstring);
fclose($feed);

?>


$sql = "INSERT INTO $maintable (ProductID, Name, MerchantID, Merchant, Link, Thumbnail, BigImage, Price, RetailPrice, Category, SubCategory, Description, Custom1, Custom2, Custom3, LastUpdated, status) VALUES ({$ProductID}, {$Name}, {$MerchantID}, {$Link}, {$Thumbnail}, {$BigImage}, {$Price} {$RetailPrice}, {$Category}, {$title}, {$SubCategory}, {$Description}, {$Custom1}, {$Custom2}, {$Custom3}, {$LastUpdated}, {$status}) ON DUPLICATE KEY UPDATE ProductID={$ProductID}, Name={$Name}, MerchantID={$MerchantID}, Merchant={$Merchant}, Link={$Link}, Thumbnail={$Thumbnail}, BigImage={$BigImage}, RetailPrice={$RetailPrice}, Category={$Category}, SubCategory={$SubCategory}, Description={$Description}, Custom1={$Custom1}, Custom2={$Custom2}, Custom3={$Custom3}, LastUpdate={$LastUpdate}, status={$status}");
echo "SQL Statement = $sql <br />";
$result = mysql_query($sql);

By the way, I don’t understand why you put all those curly brackets around the variables in the sql statement, and I think you’re missing some single quotes there.

I’m just sticking this straight into phpmyadmin. Here’s what I’m doing, I don’t know if it is correct but it makes a little better sense. I load the table with another script. Then I use this script to update from the other table.

Anyway, when I run this statement in phpmyadmin I get Error! Error! Error! Will Robertson >>> #1241 - Operand should contain 1 column(s)

I looked up “Operand” and it appears that is the + in 2+2 …not much help. What is wrong now. Thanks

INSERT INTO naturehills( ProductID, Name, MerchantID, Merchant, Link, Thumbnail, BigImage, Price, RetailPrice, Category, SubCategory, Description, Custom1, Custom2, Custom3, LastUpdated )
SELECT (
ProductID, Name, MerchantID, Link, Thumbnail, BigImage, Price, RetailPrice, Category, SubCategory, Description, Custom1, Custom2, Custom3, LastUpdated
)
FROM naturehills_load ON DUPLICATE
KEY UPDATE ProductID = ProductID,
Name = Name,
MerchantID = MerchantID,
Merchant = Merchant,
Link = Link,
Thumbnail = Thumbnail,
BigImage = BigImage,
Price = Price,
RetailPrice = RetailPrice,
Category = Category,
SubCategory = SubCategory,
Description = Description,
Custom1 = Custom1,
Custom2 = Custom2,
Custom3 = Custom3,
LastUpdate = LastUpdate 

your SELECT clause is wrong – you’re not allowed to put the list of columns into parentheses

that only works if you’re using the VALUES option

also, the ON DUPLICATE part is wrong as well, you’d be setting every column equal to what it was, and mysql ignores that