Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem while updating database 1

Status
Not open for further replies.

sun11

Programmer
Dec 22, 2009
21
0
0
GB
Hi,

I have a database called nielupdate and i want to update the database. the rows which are undated are stored in a text file. so i need to update from the text file to the database. iam not able to do that ...
i have given the code which i used



<?php

session_start();
include("../connect2db.php");

//$user_descrip = $_SESSION['user_descrip'];
$user_descrip="Administrator";
if($user_descrip!="Administrator")
{
header("Location: ../accessdenied.htm");
}
else
{
$table="nielupdate";

//AF
$sql="update $table set custdept='AF' where substring(BIC1,1,1)='F'";
mysql_query($sql);

//JF
$sql="update $table set custdept='JF' where substring(BIC1,1,1)='Y' and substring(BIC1,2,1)='F'";
mysql_query($sql);

//JNF
$sql="update $table set custdept='JNF' where substring(BIC1,1,1)='Y' and substring(BIC1,2,1)!='F'";
mysql_query($sql);

//ANF
$sql="update $table set custdept='ANF' where custdept='' and substring(BIC1,1,1)!='Y' and substring(BIC1,1,1)!='F' and BIC1!=''";
mysql_query($sql);

//Binding Paperback
$sql="update $table set binding='Paperback' where binding='' or binding= 'NULL' and substring(FMC,1,2)='BC'";
mysql_query($sql);

//Binding Hardback
$sql="update $table set binding='Hardback' where binding='' or binding= 'NULL' and substring(FMC,1,2)='BB'";
mysql_query($sql);

//JF
$sql="update $table set custdept='JF' where custdept='' and substring(dw,1,1)='8' and substring(dw,3,1)='3' and (substring(rc,1,1)='S' or substring(rc,1,1)='J' or substring(rc,1,1)='X')";
mysql_query($sql);


//AF
$sql="update $table set custdept='AF' where custdept='' and substring(dw,1,1)='8' and substring(dw,3,1)='3' and (substring(rc,1,1)!='S' or substring(rc,1,1)!='J' or substring(rc,1,1)!='X')";
mysql_query($sql);

//JNF
$sql="update $table set custdept='JNF' where custdept='' and substring(dw,1,1)!='8' and substring(dw,3,1)!='3' and dw!='' and (substring(rc,1,1)='S' or substring(rc,1,1)='J' or substring(rc,1,1)='X')";
mysql_query($sql);

//ANF
$sql="update $table set custdept='ANF' where custdept='' and substring(dw,1,1)!='8' and substring(dw,3,1)!='3' and dw!='' and (substring(rc,1,1)!='S' or substring(rc,1,1)!='J' or substring(rc,1,1)!='X')";
mysql_query($sql);


//fiction Y
$sql="update $table set fiction='Y' where custdept='AF' or custdept='JF'";
mysql_query($sql);

//fiction N
$sql="update $table set fiction='N' where custdept!='AF' and custdept!='JF'";
mysql_query($sql);

//copy date
$sql="update $table set pub_date=PDUK";
mysql_query($sql);

//fix type HW281208
$sql="update $table set type1='BOOKS' where type1=''";
mysql_query($sql);

//Update type1 and binding to BOOKS
$sql0="select catalog_no,fmc from $table";
$res0=mysql_query($sql0);
while($ser0=mysql_fetch_array($res0))
{
$fmc=$ser0['fmc'];
$catalog_no=$ser0['catalog_no'];
$num_rows = mysql_num_rows(mysql_query("select type1 from neilsen_codes where fmc='$fmc'"));
If ($num_rows > 0)
{
$type1=mysql_result(mysql_query("select type1 from neilsen_codes where fmc='$fmc'"),0);
}
$num_rows = mysql_num_rows(mysql_query("select binding from neilsen_codes where fmc='$fmc'"));
If ($num_rows > 0)
{
$binding=mysql_result(mysql_query("select binding from neilsen_codes where fmc='$fmc'"),0);
}
mysql_query("update $table set type1='$type1' where catalog_no='$catalog_no'");
mysql_query("update $table set binding='$binding' where catalog_no='$catalog_no'");
}
//update the entire table
$sql="load data infile W:\Nielsen13_Data\upd_20090527c.txt replace into table $table";
//$sql="insert ignore into $table select * from W:\Nielsen13_Data\upd_20090527c.txt";
//$sql="INSERT INTO $table SELECT ON DUPLICATE KEY UPDATE W:\Nielsen13_Data\upd_20090527c.txt";
mysql_query($sql);
if ($table != 0)
{
echo 'Updated';
}
else
{
echo 'Not Updated';
}

}
?>

thanks,
sun11
 
(First of all, I don't think I'll be able to help you directly with this - just hoping to do a little learning on my own.)

So, with that, have a little understanding if my question seems like an ignoramus.

My first question on this is: Tell us a little bit about the text file. Is it a table in text format? Is it a standard .txt file, or is it a .csv file or other? Is the data set in column/row order, such as a delimited exported file from most databases?

I'll see if I can look a little and see if anything sticks out to me, but I ain't promisin' nothin'! [wink]

--

"If to err is human, then I must be some kind of human!" -Me
 
Exibit 1 of my ignorance of php to date: A question I have:

You start out with this code:
Code:
//$user_descrip = $_SESSION['user_descrip'];
$user_descrip="Administrator";
if($user_descrip!="Administrator")
{
 header("Location: ../accessdenied.htm");
}
else
{

It may be that I'm somehow missing the logic, but it looks like you're saying here that if the user is logged in as Administrator, give them the 'access denied' message, but otherwise proceed. So obviously I'm at a loss right off the bat. I would think you'd want the administrator account to be the one allowed for such an action, and all else be denied. It's probably something I'm not understanding in the code. I've looked at a wee little bit to date, and changed a few tiny things here and there for a couple Drupal sites, but I mainly let Drupal take care of the code - so far - in those instances.

--

"If to err is human, then I must be some kind of human!" -Me
 
And if I'm following your original question as well as your code correctly, in the end, you're getting the message, "Not Updated."

Could it possibly be something as simple as some pattern checking you need to run against the text file before attempting to import it?

--

"If to err is human, then I must be some kind of human!" -Me
 
@kjv611:
Code:
$user_descrip="Administrator";
if($user_descrip!="Administrator")
{
 header("Location: ../accessdenied.htm");
}
else
{

the code is correct. If you look at the if statement, you'll notice that its not checking to see whether $user_descript is equal to "Administrator" but rather if its not equal to it.

The exclamation point there turns the equal sign into "Not Equal".
so that part right as it will move on if it is equal, instead redirecting to the access denied.

@sun11

Your last If statement will always be false. That is $table can never be 0 since you've previously set it to be "nielupdate"

So for that you should always get "Updated" regardless of what happens in the query.

Updating requires existing records, your query as it is will jus dump the information in the text file into the table replacing anything already there.

You'd need to read the file and create updates statements based on the information.

We do need to see the format of the file, as well as the table structure to be able to help more.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks, Vacunita! I knew I was missing something, but wanted to make sure! I actually knew about the ! being "not", but just totally forgot and overlooked it.

I got a long way to go with learning any new language, just not enough time to do it, really... hopefully I'll get things sorted out enough to get more done? Maybe... maybe not. [smile]

--

"If to err is human, then I must be some kind of human!" -Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top