Forums / General Questions / Desperatley Need Help with PHP / MYSQL code

Desperatley Need Help with PHP / MYSQL code

Locked

Views: 2,109

Results 1 to 20 of 20
This thread is locked. New replies are disabled.
6 Oct 2009, 5:35 PM
#1
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Desperatley Need Help with PHP / MYSQL code

Hi everyone.

Please Please Please can someone help me?

I am trying to set up a php page that updates product qty's in the database. I almost have it working, however seem to have hit a brick wall.

The php simply reads an XML file which contains Product Codes & Qtys, and then $variable echo's are used in the mysql queery to UPDATE the database.

All works perfectly apart from when I have a product code that is alpha numeric.

For Example, My php file reads the XML and will find product codes as they are in the XML: i.e $productcode will echo 28923 or 28374S.

Now, the mysql syntax im using that works fine with numbers is:

UPDATE products_with_attributes_stock SET quantity = 100 WHERE product_code = 28923

This updates the quatity as it should as does the PHP file for any $productcode that echo's as just a number with:

mysql_query("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = $productcode");

What doesnt work is:

UPDATE products_with_attributes_stock SET quantity = 100 WHERE product_code = 28374S

this returns the error -


Error

SQL query:

UPDATE products_with_attributes_stock SET quantity =100 WHERE product_code = 28374S

MySQL said: Documentation
#1054 - Unknown column 'product_code' in 'where clause'


the value 28374S does exist! - I dont understand why mysql is returning this result..

I have noted that if I repeat the syntax with double quotations i.e "28734S" it works! BUT this is no good as my $productcode is not echoing that!???

In addition, I have managed to make my php echo the codes in double quotatons by doing $quote = '"'.$productcode.'"'; and using the following code for the syntax:

mysql_query("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = $quote");

BUT THIS ALSO DOESNT WORK!

Please please please let there be someone out there that can tell me why this isn't working!???

Is it my php code? is it the syntax? is it the way I have my table set up?? IM STUCK..

Kind Regards
Phil
:cry:

Phil Rogers
A problem shared is a problem solved.

6 Oct 2009, 6:04 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

Wrapping quote-marks around alphanumeric strings is absolutely important. A pair of single-quotes or a pair of double-quotes will suffice. Single is the usual approach when using strings.

Also make sure your custom added "product_code" field exists in the table and that it's not a numeric/integer type. If it needs to handle strings it will need to be a varchar or char type (or text if it's huge data).

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

6 Oct 2009, 6:21 PM
#3
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

DrByte:

Wrapping quote-marks around alphanumeric strings is absolutely important. A pair of single-quotes or a pair of double-quotes will suffice. Single is the usual approach when using strings.

Also make sure your custom added "product_code" field exists in the table and that it's not a numeric/integer type. If it needs to handle strings it will need to be a varchar or char type (or text if it's huge data).

OK - so I definatley need double quotes.
Any ideas as to why when I use the $quote, which echos "24437S " it still doesnt work?

is it my php code that is incorrect in the syntax string part ? How can I write it so it adds quotations to the queery?

The custom field does exist and it is set to:

product_code varchar(32) latin1_swedish_ci No

I am desperate to get the php to run this queery and need a workaround.

Thanks for you quick response, any further suggestions/advice would be greatly appreciated.

Phil

Phil Rogers
A problem shared is a problem solved.

6 Oct 2009, 6:36 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

philip937:

Now, the mysql syntax im using that works fine with numbers is:

UPDATE products_with_attributes_stock SET quantity = 100 WHERE product_code = 28923

This updates the quatity as it should as does the PHP file for any $productcode that echo's as just a number with:

mysql_query("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = $productcode");

What doesnt work is:

UPDATE products_with_attributes_stock SET quantity = 100 WHERE product_code = 28374S

  1. I don't use "mysql_query()" when running inside Zen Cart, because the database abstraction layer inside ZC is smarter and safer because there are numerous data-typing (aka security) protection options available in it, etc.

  2. I always refrain from embedding variables inside double-quotes. While it's technically accepted, it can make for confusion when troubleshooting something that's going wrong. To keep it clear what's doing what, I always concatenate strings and vars separately instead. You'll see that pattern consistently throughout the Zen Cart code too.

  3. Since you want to use mysql_query() directly, this syntax should work just fine:```
    mysql_query("UPDATE products_with_attributes_stock SET quantity = " . $stock . " WHERE product_code = '" . $productcode . "'");

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

6 Oct 2009, 7:02 PM
#5
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

DrByte:

mysql_query("UPDATE products_with_attributes_stock SET quantity = " . $stock . " WHERE product_code = '" . $productcode . "'");



Thanks again for your quick response.  That code didn't seem to work, this time it didnt even UPDATE to Number only codes?  Any thing else I can try??

Phil Rogers
A problem shared is a problem solved.

6 Oct 2009, 7:06 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

You didn't mention any error message, so I can only assume it ran without one.
If it's not working with that, then I'd be suspecting that the PHP logic you're using is faulty or your input data is bad ... or there are no relevant records to update.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

6 Oct 2009, 7:07 PM
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

What if you replace the "mysql_query" call with just an "echo" call? ie: output the generated SQL to the screen, to see what it's really doing?
Then what happens if you run that SQL in phpMyAdmin? What happens?

Both of those steps can be very revealing of where you've got logic or syntax errors.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

6 Oct 2009, 7:30 PM
#8
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

DrByte:

What if you replace the "mysql_query" call with just an "echo" call? ie: output the generated SQL to the screen, to see what it's really doing?
Then what happens if you run that SQL in phpMyAdmin? What happens?

Both of those steps can be very revealing of where you've got logic or syntax errors.

the following is my php code, granted im a novice and its been peiced together form examples off the net. perhaps you could validate?

<?php

$con = mysql_connect('localhost', '<username>', '<password>);
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('overplan_mirror');



$rssFeeds = array ('supplier.xml');

//Loop through the array, reading the feeds one by one
foreach ($rssFeeds as $feed) {
  readFeeds($feed);
}
function startElement($xp,$name,$attributes) {  
global $item,$currentElement;  $currentElement = $name; 
//the other functions will always know which element we're parsing  
if ($currentElement == 'PRODUCT') { 
//by default PHP converts everything to uppercase    
$item = true; 
// We're only interested in the contents of the item element. 
////This flag keeps track of where we are  
}}

function endElement($xp,$name) {  
global $item,$currentElement,$productcode,$stock,$duedate;$conn;    
if ($name == 'PRODUCT') { 
// If we're at the end of the item element, display 
// the data, and reset the globals    
echo "<b>Product Code:</b> $productcode<br>";    
echo "<b>Stock:</b> $stock<br>";    
echo "<b>Due Date:</b> $duedate<br><br>";  
  $ins_product = addslashes($productcode);
  $ins_qty = addslashes($stock);
  $ins_due = addslashes($duedate);  



mysql_query("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = $productcode");

$productcode = '';
$stock = '';    
$duedate = '';    
$item = false;  }}



function characterDataHandler($xp,$data) {  
global $item,$currentElement,$productcode,$stock,$duedate;    
if ($item) { 
//Only add to the globals if we're inside an item element.    
switch($currentElement) {      
case "PRODUCT_CODE":        
$productcode .= $data; 
// We use .= because this function may be called multiple 
// times for one element.        
break;      
case "AVAILABLE_STOCK":        
$stock.=$data;        
break;      
case "DUE_DATE":        
$duedate.=$data;        
break;     }  }}



function readFeeds($feed) {
  $fh = fopen($feed,'r'); 
// open file for reading

  $xp = xml_parser_create(); 
// Create an XML parser resource

  xml_set_element_handler($xp, "startElement", "endElement"); 
// defines which functions to call when element started/ended

  xml_set_character_data_handler($xp, "characterDataHandler");

  while ($data = fread($fh, 4096)) {
    if (!xml_parse($xp,$data)) {
      return 'Error in the feed';
    }
  }
}
?>

Also supplier.xml is in the following format:

<StockValues> − <Product> <Product_Code>00380 </Product_Code> <Available_Stock>3960</Available_Stock> <Due_Date>N/A</Due_Date> </Product> − <Product> <Product_Code>00429 </Product_Code> <Available_Stock>4285</Available_Stock> <Due_Date>N/A</Due_Date> </Product> − <Product> <Product_Code>00495 </Product_Code> <Available_Stock>1035</Available_Stock> <Due_Date>N/A</Due_Date> </Product> − <Product> </StockValues>

**The Following is the php output echo from the code above as it is
mysql_query("UPDATE products_with_attributes_stock SET quantity = $stock WHERE smiffy_id = $productcode");

Product Code: 00380
Stock: 3960
Due Date: N/A

Product Code: 00429
Stock: 4285
Due Date: N/A

Product Code: 00495
Stock: 1035
Due Date: N/A

Product Code: 0122
Stock: 2953
Due Date: N/A

Product Code: 0190
Stock: 3434
Due Date: N/A

Product Code: 052A
Stock: 550
Due Date: N/A

When I echo the queery*****:

echo = ("UPDATE products_with_attributes_stock SET quantity = $stock WHERE smiffy_id = $productcode");

UPDATE products_with_attributes_stock SET quantity = 789 WHERE product_code = 20320 Product Code: 20389L
Stock: 11
Due Date: N/A

UPDATE products_with_attributes_stock SET quantity = 11 WHERE product_code = 20389L Product Code: 20389M
Stock: 161
Due Date: N/A

UPDATE products_with_attributes_stock SET quantity = 161 WHERE product_code = 20389M Product Code: 20390M
Stock: 87
Due Date: N/A

UPDATE products_with_attributes_stock SET quantity = 87 WHERE product_code = 20390M Product Code: 20390S
Stock: 0
Due Date: N/A

UPDATE products_with_attributes_stock SET quantity = 0 WHERE product_code = 20390S Product Code: 20392L
Stock: 160
Due Date: N/A

Im just about to echo the code you sent to see what that shows..

Phil Rogers
A problem shared is a problem solved.

6 Oct 2009, 7:33 PM
#9
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

Your code gives:
echo ("UPDATE products_with_attributes_stock SET quantity = " . $stock . " WHERE product_code = '" . $productcode . "'");

UPDATE products_with_attributes_stock SET quantity = 789 WHERE product_code = '20320 'Product Code: 20389L
Stock: 11
Due Date: N/A

UPDATE products_with_attributes_stock SET quantity = 11 WHERE product_code = '20389L 'Product Code: 20389M
Stock: 161
Due Date: N/A

UPDATE products_with_attributes_stock SET quantity = 161 WHERE product_code = '20389M 'Product Code: 20390M
Stock: 87
Due Date: N/A

Phil Rogers
A problem shared is a problem solved.

6 Oct 2009, 9:27 PM
#10
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

DrByte,

Just for clarity to save confusion, I have modified the php I have and got rid of some of the unnecessary echos.

Below is what I now have for my php:

<?php

$con = mysql_connect('localhost', '<username>', '<password>');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected Successfully<br>';
mysql_select_db('overplan_mirror');



$rssFeeds = array ('supplier.xml');

//Loop through the array, reading the feeds one by one
foreach ($rssFeeds as $feed) {
  readFeeds($feed);
}
function startElement($xp,$name,$attributes) {  
global $item,$currentElement;  $currentElement = $name; 
//the other functions will always know which element we're parsing  
if ($currentElement == 'PRODUCT') { 
//by default PHP converts everything to uppercase    
$item = true; 
// We're only interested in the contents of the item element. 
////This flag keeps track of where we are  
}}

function endElement($xp,$name) {  
global $item,$currentElement,$productcode,$stock,$duedate;$conn;    
if ($name == 'PRODUCT') { 
// If we're at the end of the item element, display 
// the data, and reset the globals    


  $ins_product = addslashes($productcode);
  $ins_qty = addslashes($stock);
  $ins_due = addslashes($duedate);  


$sql = ("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = \"$productcode\"");

echo "UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = \"$productcode\"<br>";

$productcode = '';
$stock = '';    
$duedate = '';    
$item = false;  }}



function characterDataHandler($xp,$data) {  
global $item,$currentElement,$productcode,$stock,$duedate;    
if ($item) { 
//Only add to the globals if we're inside an item element.    
switch($currentElement) {      
case "PRODUCT_CODE":        
$productcode .= $data; 
// We use .= because this function may be called multiple 
// times for one element.        
break;      
case "AVAILABLE_STOCK":        
$stock.=$data;        
break;      
case "DUE_DATE":        
$duedate.=$data;        
break;     }  }}



function readFeeds($feed) {
  $fh = fopen($feed,'r'); 
// open file for reading

  $xp = xml_parser_create(); 
// Create an XML parser resource

  xml_set_element_handler($xp, "startElement", "endElement"); 
// defines which functions to call when element started/ended

  xml_set_character_data_handler($xp, "characterDataHandler");

  while ($data = fread($fh, 4096)) {
    if (!xml_parse($xp,$data)) {
      return 'Error in the feed';
    }
  }
}
?> 

The result on database is the same, numbers updated, alphanumeric not.

The echo result of php file is:

Connected Successfully
UPDATE products_with_attributes_stock SET quantity = 3960 WHERE product_code = "00380 "
UPDATE products_with_attributes_stock SET quantity = 4285 WHERE product_code = "00429 "
UPDATE products_with_attributes_stock SET quantity = 1035 WHERE product_code = "00495 "
UPDATE products_with_attributes_stock SET quantity = 2953 WHERE product_code = "0122 "
UPDATE products_with_attributes_stock SET quantity = 3434 WHERE product_code = "0190 "
UPDATE products_with_attributes_stock SET quantity = 550 WHERE product_code = "052A "
UPDATE products_with_attributes_stock SET quantity = 1479 WHERE product_code = "052B "
UPDATE products_with_attributes_stock SET quantity = 5206 WHERE product_code = "052BK "
UPDATE products_with_attributes_stock SET quantity = 1956 WHERE product_code = "052G "
UPDATE products_with_attributes_stock SET quantity = 3230 WHERE product_code = "052GN "
UPDATE products_with_attributes_stock SET quantity = 876 WHERE product_code = "052L "

as you can see I changed the sql queery to:
$sql = ("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = "$productcode"");

this seem's to give the desired echo output, but STILL does not update the quantitys associated with the alphanumeric product_code entries,,,

Am I still missing something / doing something wrong here?????

Kind Regards,
Phil :cry:

Phil Rogers
A problem shared is a problem solved.

7 Oct 2009, 4:37 AM
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

All your product_codes inside the quotes have a space on the end. Is that how they're actually stored in the database?
Maybe you need to clean the data you're getting from your xml source before processing it?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

7 Oct 2009, 7:09 AM
#12
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

DrByte:

All your product_codes inside the quotes have a space on the end. Is that how they're actually stored in the database?
Maybe you need to clean the data you're getting from your xml source before processing it?

I also noticed this and thought it was strange. I tested the syntax with a space manually by copying and pasting into myphp admin in the database and it worked, but not from the php page, would something be happening with that space that I don't see that stops the syntax from working from the php page?

If so, any suggestions on how I could remove it? Is it the way my php file is Reading it?

Phil
Posted via Mobile Device

Phil Rogers
A problem shared is a problem solved.

7 Oct 2009, 9:49 AM
#13
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

philip937:

I also noticed this and thought it was strange. I tested the syntax with a space manually by copying and pasting into myphp admin in the database and it worked, but not from the php page, would something be happening with that space that I don't see that stops the syntax from working from the php page?

If so, any suggestions on how I could remove it? Is it the way my php file is Reading it?

Phil
Posted via Mobile Device

As a test I set up the following:

$test = 35848M;

I replaced $productcode with $test and ran again, the echo was " 35848M" with no space at the end.
The queery still didn't update the quantity for 35848M!!

Arghhh why won't this work?
Posted via Mobile Device

Phil Rogers
A problem shared is a problem solved.

7 Oct 2009, 11:52 AM
#14
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

" 35848M" is not the same as "35848M"
"00380 " is not the same as "00380"
"052BK " is not the same as "052BK"

If your incoming data feed is sloppy and contains spaces, then you may need to trim those spaces off before using the data in your queries.
Perhaps like this:```
$productcode = trim($productcode);

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

7 Oct 2009, 11:54 AM
#15
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

Unrelated to your "spaces" issue, I noticed this in your code above:> philip937:

$ins_product = addslashes($productcode);
$ins_qty = addslashes($stock);
$ins_due = addslashes($duedate);


You've set values for $ins_product (etc), but you're not actually using those in your queries.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

7 Oct 2009, 4:19 PM
#16
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

DrByte:

Unrelated to your "spaces" issue, I noticed this in your code above:

You've set values for $ins_product (etc), but you're not actually using those in your queries.

im not sure what the purpose of these were. they were just in the examples pulled from the net so left them in.

I may have made a breakthrough although Im not understanding properly, maybe you would be able to figure out for me?

I contacted the people that provide the data feed for xml for me. on the face of it it sounds like your suspition of there being "bad" data could be right, though im not sure how to progress. They sent me the following:


The problem is that in the XML file in the <Product_Code> tag there is 11 blank spaces.
In the PHP you need to eliminate all the blank spaces with a

$new_string = str_replace(' ','',$each_product_code); <<<the first '' has 11 spaces before i pasted into here...

for each product code.

And use:
$sql = 'UPDATE products_with_attributes_stock SET quantity = '.$stock.' WHERE product_code = ''.$productcode.'' ');

For the MySQL query.


Im not sure what I am supposed to do with this... I pasted it in but im sure thats not what they mean to do at all?? can you advise.. does this make sense to you?? are you able to advise how I should use this in my php file?

Kind Regards,

Phil

Phil Rogers
A problem shared is a problem solved.

7 Oct 2009, 4:33 PM
#17
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

Their response does confirm that they include spaces in their data. Not sure exactly why they do that, but I'm sure they have their reasons.
Their suggestion using str_replace will remove all spaces from the $each_product_code variable value. But using str_replace takes more CPU processing than trim() does. The down-side is that trim() only deals with leading and trailing spaces, and not with spaces inside the string, ie: "123 456".

My suggestion would be to change this part of the code you posted earlier from this:```
$ins_product = addslashes($productcode);
$ins_qty = addslashes($stock);
$ins_due = addslashes($duedate);

$sql = ("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = "$productcode"");

echo "UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = "$productcode"<br>";
to this:
$ins_product = addslashesB[/B];
$ins_qty = addslashes($stock);

$[B]sql = "UPDATE[/B] products_with_attributes_stock SET quantity = " . (int)$stock . " WHERE product_code [B]= '" . $ins_product . "'";[/B]

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

7 Oct 2009, 5:23 PM
#18
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

:clap::clap::clap::clap::clap:

Fantastic! I finally have it working!!!! All Product codes now potulating with new quantities!!!!!!!! Whoohoooo

NEW PROBLEM NOW :no:

Now I have poulated my products_with_attributes_stock table, all the values are ther perfect, and when I look at Catalogue/Product with Atributes Stock they are all populated.

However All products are showing as out of stock?????

is it something to do with the syncing of quantitys?? as the top of each product it says Quantity for all variants, all are 0. I guessing to get it qorking all need to be synced???

Problem is I have over 3000 products, is there a way to sync these using a query in the database!... oooo im almost there! thanks for all your help so far!

Regards,
Phil

Phil Rogers
A problem shared is a problem solved.

7 Oct 2009, 5:35 PM
#19
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Desperatley Need Help with PHP / MYSQL code

Sorry, I'm not versed in the products_with_attributes_stock addon's architecture. It's different than the core Zen Cart use. You'll have to study the addon to understand how it works and fudge your data accordingly.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

7 Oct 2009, 5:47 PM
#20
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Desperatley Need Help with PHP / MYSQL code

Ok, thanks for all your help, I think i need to work out what the button does to the db and try and run a queey that aplies it to all..

THANKS :D

Phil Rogers
A problem shared is a problem solved.