Google Answers Logo
View Question
 
Q: Storing data into MySQL table after pulling it from MySQL and cleaning with PHP ( No Answer,   2 Comments )
Question  
Subject: Storing data into MySQL table after pulling it from MySQL and cleaning with PHP
Category: Computers > Programming
Asked by: throcko-ga
List Price: $30.00
Posted: 04 Apr 2006 01:43 PDT
Expires: 15 Apr 2006 12:20 PDT
Question ID: 715251
SUMMARY OF WHAT I'M TRYING TO DO:

I'm simply trying to use PHP to pull the data out of a MySQL table,
"clean" the data values, then store the "cleaned" data back into the
original table (although I'm willing to create another table with
identical structure and store the cleaned data there if necessary).

The "cleaning" that I'm trying to do consists of stripping any double
quotes in the first or last character positions and stripping any
dollar signs from the value in every field in the table (including the
primary key fields).  I'd prefer to store the cleaned data back into
the original row (although I'm willing to store into a new row and
delete the original row).


===================================================================


SOME MORE DETAILS:

I've been able to successfully pull the data out of the table and
strip off the unwanted characters (with the exception that I'm
stripping double quotes in all positions rather than just those in the
leading or trailing character positions).  However, I can't figure out
how to store the data back into the table.  Maybe I've just got syntax
errors in the lines where I'm attempting to write the SQL query, but
if so, I can't figure it out.

By the way, I want to clean the data in numerous tables, and I will
have some tables in which the primary key values are strings and
contain double quotes, although all of my attempts so far have been
with a table that has no unwanted characters in the primary key
fields.  In all of my attempts, I've been unsuccessful in storing the
data back into the table.

The MySQL version that is available to me is 4.1.14.  I've been using
phpMyAdmin 2.6.3-pl1 and PHP version 4.3.11.
The PHP code that I've included below shows 4 different regions of
now-commented-out code where I've unsuccessfully tried to construct
code that stores data back into the MySQL table.  These 4 regions
begin with the phrases "REGION 1", "REGION 2", "REGION 3", and "REGION
4".


===================================================================

WHAT I NEED AS AN ANSWER:

I need one of 2 things:

-- To know the proper PHP/MySQL statements that will allow me to
store, back into a MySQL table, the cleaned data that my existing code
generates.

OR

-- To be given the code for a different approach that will pull the
data from the MySQL table, clean it, then store it back into the MySQL
table.



===================================================================

HERE IS THE DEFINITION AND CONTENTS OF THE TABLE THAT I'VE BEEN WORKING WITH:

CREATE TABLE YSB_Items (
	Order_ID	INTEGER(10) UNSIGNED NOT NULL,
	Line_ID		TINYINT UNSIGNED NOT NULL,
	Product_ID	VARCHAR(30),
	Product_Code	VARCHAR(30),
	Quantity	SMALLINT UNSIGNED NOT NULL,
	Unit_Price	VARCHAR(7),
PRIMARY  KEY(Order_ID,Line_ID)   );


DATA IN THE TABLE:

500,1,"p010","P010",1,$9.95
500,2,"Tax","Tax",1,$0.63
500,3,"Shipping","Shipping",1,$1.00
501,1,"p010","P010",1,$9.95
501,2,"Tax","Tax",1,$0.00
501,3,"Shipping","Shipping",1,$1.00
502,1,"p005","P005",1,$7.50
502,2,"Tax","Tax",1,$0.47
502,3,"Shipping","Shipping",1,$1.00
503,1,"p005","P005",1,$7.50
503,2,"Tax","Tax",1,$0.47
503,3,"Shipping","Shipping",1,$1.00
504,1,"p005","P005",1,$7.50
504,2,"Tax","Tax",1,$0.22
504,3,"Shipping","Shipping",1,$1.00
505,1,"p005","P005",1,$7.50
505,2,"Tax","Tax",1,$0.27
505,3,"Shipping","Shipping",1,$1.00
506,1,"p005","P005",1,$7.50
506,2,"Tax","Tax",1,$0.47
506,3,"Shipping","Shipping",1,$1.00
507,1,"p005","P005",1,$7.50
507,2,"p001","P001",1,$4.50
507,3,"Tax","Tax",1,$0.76
507,4,"Shipping","Shipping",1,$1.00



===================================================================

BELOW IS THE CURRENT STATE OF MY PHP CODE (It works as I expect,
except for my inability to put the cleaned data back into the table). 
The output from running this code is at the very bottom:



<!-- Program:  move_and_clean_YSB_store_data.php
     Desc:     PHP program to move data from YSB_... tables to other
tables, while doing some cleaning-up of the data as received from the
YSB store.
-->
<html>
<head><title>Move and clean data from the YSB tables.</title></head>
<body>
<?php

$host='mysql';
$user=$_POST['user'];
$password=$_POST['password'];

if (!$connection = mysql_connect($host,$user,$password))
{
	$message = mysql_error();
	echo '<p>';
	echo $message;
	die();
}

$database = 'IFYI1';
$db = mysql_select_db($database,$connection)
	or die ("Couldn't select database.");

/* --------------------------------------------------------------------*/
/* Now connected to the IFYI1 database at this point, so start queries */
/* --------------------------------------------------------------------*/


$query = 'SELECT * FROM YSB_Items';

$result = mysql_query($query, $connection)
	or die("Couldn't execute query.");
$nrows = mysql_num_rows($result);
$nfields = mysql_num_fields($result);

// Echo some values so that I'm confident that they're what I expect.
echo '<p>';
echo 'Number of rows = ';
echo $nrows;
echo '<p>';
echo 'Number of fields = ';
echo $nfields;

// Create an array with entries that are the same as the column names
in the table YSB_Items.
$field_names[0] = 'Order_ID';
$field_names[] = 'Line_ID';
$field_names[] = 'Product_ID';
$field_names[] = 'Product_Code';
$field_names[] = 'Quantity';
$field_names[] = 'Unit_Price';

// Print the array element values to verify they're what I expect.
echo "<pre>";
print_r($field_names);
echo "</pre>";


for ($row_num=0; $row_num<$nrows; $row_num++)
{
	$row = mysql_fetch_array($result, MYSQL_BOTH);
	echo '<p>';
	echo 'Processing row ';
	echo $row_num;
	echo ' :    ';
	extract($row);

	// Strip double quotes and dollar signs from all fields, then store
field back into same table.
	for ($field_num=0; $field_num<$nfields; $field_num++)
	{
		$chars_to_strip = array("\"","\$");
		$replacement_chars = array("");
		$cleaned_data[$field_num] = str_replace( $chars_to_strip,
$replacement_chars, $row[$field_num]);
		$cleaned_data[$field_num] = trim($cleaned_data[$field_num]);
		$cleaned_data[$field_num] = htmlspecialchars($cleaned_data[$field_num]);

        // Echo both the original value and the cleaned value to
verify it's what I expect
		echo '<p>';
		echo $row[$field_num];
		echo '     ';
		echo $cleaned_data[$field_num];

// REGION 1:
// THIS STUFF BELOW DOESN'T WORK AS I WANT: This is my attempt to put
the cleaned value
// back into the field from which it came in the MySQL database table.
 I'd prefer to
// replace the entire row in one operation rather than replace fields
column by column.
// However, I was also having no luck with the "replace an entire row
at once" approach,
// so I tried this column by column approach, which also isn't working for me.
//		$query = 'UPDATE YSB_Items SET $row[$field_num]=$cleaned_data[]
//			WHERE Order_ID= $row['Order_ID'] AND Line_ID=$row['Line_ID']';
//		echo '<p>';
//		echo $row[$field_num];
//		$result = mysql_query($query, $connection)
//			or die("Couldn't execute query.");

echo '<p>';
echo $field_names[$field_num];


	}


/*
	echo "<pre>";
	print_r($cleaned_data);
	echo "</pre>";
*/

// Verify that certain values are what I expected.
echo '<p>';
echo $row[0];
echo '<p>';
echo $row[1];

// REGION 2:
// ANOTHER UNSUCCESSFUL ATTEMPT AT STORING AN ENTIRE ROW AT A TIME.
// I Still Haven't been able to figure out how to store fields back
into the table!!!!!
/*
	// Store fields back into table from which they came.
	for ($field_num=0; $field_num<$nfields; $field_num++)
	{
//	$query = 'UPDATE YSB_Items SET * = $cleaned_data[$field_num]
	$query = 'UPDATE YSB_Items SET $field_names[$field_num]= $cleaned_data[$field_num]
			WHERE Order_ID= $row[0] AND Line_ID = $row[1]';
	$result = mysql_query($query
		or die("Couldn't execute query.");
	}
*/

// REGION 3:
// ANOTHER UNSUCCESSFUL ATTEMPT AT STORING AN ENTIRE ROW AT A TIME.
// Create a different table to store the data values after they've been cleaned.
// Try writing the values into a table with the same column names as
the original table.
//	for ($field_num=0; $field_num<$nfields; $field_num++)
//	{
//	$query = "INSERT YSB_Items_Cleaned $field_names[$field_num] VALUES
$cleaned_data[$field_num]";
//	$result = mysql_query($query
//		or die("Couldn't execute query.");
//	}


// REGION 4:
// ANOTHER UNSUCCESSFUL ATTEMPT AT STORING AN ENTIRE ROW AT A TIME.
/*
	$query = 'UPDATE YSB_Items SET $field_names[$field_num]= $cleaned_data[$field_num]
			WHERE Order_ID= $cleaned_data[0] AND Line_ID=$cleaned_data[1]'; //
Put cleaned row into the MySQL database.
	$result = mysql_query($query, $connection)
		or die("Couldn't execute query.");
*/

}


?>

</body></html>




===================================================================

HERE IS THE OUTPUT THAT RESULTS FROM RUNNING THE PROGRAM (it shows
that the double quotes and dollar signs are being stripped). 
Typically, if I remove the commenting symbols and try to include code
to store data back into the table, either I get a blank window as
output or I get some sensible output in the window but the data in my
MySQL table has not been altered:


Number of rows = 25

Number of fields = 6

Array
(
    [0] => Order_ID
    [1] => Line_ID
    [2] => Product_ID
    [3] => Product_Code
    [4] => Quantity
    [5] => Unit_Price
)

Processing row 0 :

500 500

Order_ID

1 1

Line_ID

"p010" p010

Product_ID

"P010" P010

Product_Code

1 1

Quantity

$9.95 9.95

Unit_Price

500

1

Processing row 1 :

500 500

Order_ID

2 2

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.63 0.63

Unit_Price

500

2

Processing row 2 :

500 500

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

500

3

Processing row 3 :

501 501

Order_ID

1 1

Line_ID

"p010" p010

Product_ID

"P010" P010

Product_Code

1 1

Quantity

$9.95 9.95

Unit_Price

501

1

Processing row 4 :

501 501

Order_ID

2 2

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.00 0.00

Unit_Price

501

2

Processing row 5 :

501 501

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

501

3

Processing row 6 :

502 502

Order_ID

1 1

Line_ID

"p005" p005

Product_ID

"P005" P005

Product_Code

1 1

Quantity

$7.50 7.50

Unit_Price

502

1

Processing row 7 :

502 502

Order_ID

2 2

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.47 0.47

Unit_Price

502

2

Processing row 8 :

502 502

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

502

3

Processing row 9 :

503 503

Order_ID

1 1

Line_ID

"p005" p005

Product_ID

"P005" P005

Product_Code

1 1

Quantity

$7.50 7.50

Unit_Price

503

1

Processing row 10 :

503 503

Order_ID

2 2

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.47 0.47

Unit_Price

503

2

Processing row 11 :

503 503

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

503

3

Processing row 12 :

504 504

Order_ID

1 1

Line_ID

"p005" p005

Product_ID

"P005" P005

Product_Code

1 1

Quantity

$7.50 7.50

Unit_Price

504

1

Processing row 13 :

504 504

Order_ID

2 2

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.22 0.22

Unit_Price

504

2

Processing row 14 :

504 504

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

504

3

Processing row 15 :

505 505

Order_ID

1 1

Line_ID

"p005" p005

Product_ID

"P005" P005

Product_Code

1 1

Quantity

$7.50 7.50

Unit_Price

505

1

Processing row 16 :

505 505

Order_ID

2 2

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.27 0.27

Unit_Price

505

2

Processing row 17 :

505 505

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

505

3

Processing row 18 :

506 506

Order_ID

1 1

Line_ID

"p005" p005

Product_ID

"P005" P005

Product_Code

1 1

Quantity

$7.50 7.50

Unit_Price

506

1

Processing row 19 :

506 506

Order_ID

2 2

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.47 0.47

Unit_Price

506

2

Processing row 20 :

506 506

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

506

3

Processing row 21 :

507 507

Order_ID

1 1

Line_ID

"p005" p005

Product_ID

"P005" P005

Product_Code

1 1

Quantity

$7.50 7.50

Unit_Price

507

1

Processing row 22 :

507 507

Order_ID

2 2

Line_ID

"p001" p001

Product_ID

"P001" P001

Product_Code

1 1

Quantity

$4.50 4.50

Unit_Price

507

2

Processing row 23 :

507 507

Order_ID

3 3

Line_ID

"Tax" Tax

Product_ID

"Tax" Tax

Product_Code

1 1

Quantity

$0.76 0.76

Unit_Price

507

3

Processing row 24 :

507 507

Order_ID

4 4

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

507

4 



===================================================================

THE END.

Request for Question Clarification by hammer-ga on 05 Apr 2006 18:30 PDT
Throcko,

I think I see what's happening here. It has to do with how you are
assembling the UPDATE queries in your "not working" section. Values
from PHP arrays (like $row[]) do not automatically resolve inside
quotes the way variables do. They need to be concatenated in with the
. operator.

To test my theory, please add this line after you assemble the UPDATE query.

echo $query;

Please post the result so I can see it.

- Hammer

Clarification of Question by throcko-ga on 08 Apr 2006 19:27 PDT
Sorry for the slow response ... even though my account is configured
to send an e-mail to me whenever there is "activity" on my account, I
don't believe that I received one.  I must have missed it when going
through my spam-filter-generated "junk" folder.

The 
echo $query;
line does show that my query is not formed as I'd like it to be when
the second line of the table is to be updated, although it was formed
as desired for the first line of the table.  My problem seems to be
that the array values I expect are not being produced properly for the
2nd and following rows.

I altered the code around the area in which the UPDATE query is formed
by using this code:

$name_of_field_to_be_updated = $field_names[$field_num];
$value_of_cleaned_field = $cleaned_data[$field_num];
$primary_key_0 = $cleaned_data[0];
$primary_key_1 = $cleaned_data[1];
	$query = "UPDATE YSB_Items SET $name_of_field_to_be_updated =
'$value_of_cleaned_field'
                WHERE Order_ID = '$primary_key_0' AND Line_ID = '$primary_key_1'";
//	$query = "UPDATE YSB_Items SET $field_names[$field_num] =
'$cleaned_data[$field_num]'
//                WHERE Order_ID = '$cleaned_data[0]' AND Line_ID =
'$cleaned_data[1]'";


    echo '<p> My resulting assembled query is:';
    echo $query;
    echo '<p>';
	$result = mysql_query($query)
        or die("Couldn't execute query.");

-----------------------------------------------------------------------

The resulting output related to approximately the first 2 rows being
processed is shown below:

Number of rows = 48

Number of fields = 6

Array
(
    [0] => Order_ID
    [1] => Line_ID
    [2] => Product_ID
    [3] => Product_Code
    [4] => Quantity
    [5] => Unit_Price
)

Order_ID: 0, Line_ID: 0, Product_ID: , Product_Code: , Quantity: 0, Unit_Price:

Processing row 0 : Order_ID: 501, Line_ID: 3, Product_ID: "Shipping",
Product_Code: "Shipping", Quantity: 1, Unit_Price: $1.00

501 501

Order_ID

3 3

Line_ID

"Shipping" Shipping

Product_ID

"Shipping" Shipping

Product_Code

1 1

Quantity

$1.00 1.00

Unit_Price

Array
(
    [0] => 501
    [1] => 3
    [2] => Shipping
    [3] => Shipping
    [4] => 1
    [5] => 1.00
)

501

3

501

Order_ID

My resulting assembled query is:UPDATE YSB_Items SET Order_ID = '501'
WHERE Order_ID = '501' AND Line_ID = '3'

3

Line_ID

My resulting assembled query is:UPDATE YSB_Items SET Line_ID = '3'
WHERE Order_ID = '501' AND Line_ID = '3'

Shipping

Product_ID

My resulting assembled query is:UPDATE YSB_Items SET Product_ID =
'Shipping' WHERE Order_ID = '501' AND Line_ID = '3'

Shipping

Product_Code

My resulting assembled query is:UPDATE YSB_Items SET Product_Code =
'Shipping' WHERE Order_ID = '501' AND Line_ID = '3'

1

Quantity

My resulting assembled query is:UPDATE YSB_Items SET Quantity = '1'
WHERE Order_ID = '501' AND Line_ID = '3'

1.00

Unit_Price

My resulting assembled query is:UPDATE YSB_Items SET Unit_Price =
'1.00' WHERE Order_ID = '501' AND Line_ID = '3'

Order_ID: 501, Line_ID: 3, Product_ID: "Shipping", Product_Code:
"Shipping", Quantity: 1, Unit_Price: $1.00

Processing row 1 : Order_ID: 0, Line_ID: 0, Product_ID: ,
Product_Code: , Quantity: 0, Unit_Price:

Order_ID

Line_ID

Product_ID

Product_Code

Quantity

Unit_Price

Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
)

Order_ID

My resulting assembled query is:UPDATE YSB_Items SET Order_ID = ''
WHERE Order_ID = '' AND Line_ID = ''

Line_ID

My resulting assembled query is:UPDATE YSB_Items SET Line_ID = ''
WHERE Order_ID = '' AND Line_ID = ''

Product_ID

My resulting assembled query is:UPDATE YSB_Items SET Product_ID = ''
WHERE Order_ID = '' AND Line_ID = ''

Product_Code

My resulting assembled query is:UPDATE YSB_Items SET Product_Code = ''
WHERE Order_ID = '' AND Line_ID = ''

Quantity

My resulting assembled query is:UPDATE YSB_Items SET Quantity = ''
WHERE Order_ID = '' AND Line_ID = ''

Unit_Price

My resulting assembled query is:UPDATE YSB_Items SET Unit_Price = ''
WHERE Order_ID = '' AND Line_ID = ''

Order_ID: 0, Line_ID: 0, Product_ID: , Product_Code: , Quantity: 0, Unit_Price:

Processing row 2 : Order_ID: 0, Line_ID: 0, Product_ID: ,
Product_Code: , Quantity: 0, Unit_Price:

Order_ID

Line_ID

Product_ID

Product_Code

Quantity

Unit_Price

Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
)

Order_ID

My resulting assembled query is:UPDATE YSB_Items SET Order_ID = ''
WHERE Order_ID = '' AND Line_ID = ''

Line_ID

My resulting assembled query is:UPDATE YSB_Items SET Line_ID = ''
WHERE Order_ID = '' AND Line_ID = ''

Product_ID

My resulting assembled query is:UPDATE YSB_Items SET Product_ID = ''
WHERE Order_ID = '' AND Line_ID = ''

Product_Code

My resulting assembled query is:UPDATE YSB_Items SET Product_Code = ''
WHERE Order_ID = '' AND Line_ID = ''

Quantity

My resulting assembled query is:UPDATE YSB_Items SET Quantity = ''
WHERE Order_ID = '' AND Line_ID = ''

Unit_Price

My resulting assembled query is:UPDATE YSB_Items SET Unit_Price = ''
WHERE Order_ID = '' AND Line_ID = ''

Order_ID: 0, Line_ID: 0, Product_ID: , Product_Code: , Quantity: 0, Unit_Price: 

---------------------------------------------------------------------

Is it clear to you why I'm not getting the array values that I'd hoped?

Thanks,
Paul T.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Storing data into MySQL table after pulling it from MySQL and cleaning with
From: raymond99-ga on 05 Apr 2006 09:04 PDT
 
You don't *need* to pull the data in using MySQL. A single SQL query
seems to do the job quite nicely:

UPDATE YSB_Items
SET    Product_ID   = TRIM('"' FROM Product_ID),
       Product_Code = TRIM('"' FROM Product_Code),
       Unit_Price   = TRIM(LEADING '$' FROM Unit_Price);

I've tried it on MySQL 4.0 but assume this works on other versions just as well.

Hope this helps.
Subject: Re: Storing data into MySQL table after pulling it from MySQL and cleaning with
From: throcko-ga on 08 Apr 2006 19:29 PDT
 
Sorry for the slow response!  I seem to have an account configuration
problem or I missed the e-mail that would have notified me when you
posted your response.

Your suggestion seems to do what I need.  Thanks a bunch!

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy