Updating the database

Hi all,

I have a datebase and i want to update it. I have a text file and i have to update the database using the text file. If there is same record in the text file and the database it has to over write if the record is not there in the database but there in the text file it has to add in the database. Can any one suggest me how to do that as iam new to PHP. I am pasting the code here

<?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’”);
}
}
?>

Thanks,
Sunil

Hi Sunil,

Welcome to Sitepoint Forums!

BTW, how does your file look like (i mean how does your file stores the data) and what is your table structure? I could not come to understand from the script that you have posted above.

Hi raju,

I have so many fields in the table called nielupdate. it has catalogno,title,author and so on…

What i did was… the data is stored in the same format in the text file. the structure of the table and the text file are same. the updated data is taken and stored in a text file and i want to then update it tto the main table i.e nielupdate. there are around 1000 records. If the record in the text file and the table nielupdate is same it shold overwrite in the table . If the records are not same it should add in the table . i am not getting any clue how to do it…

thanks,
Sunil

catalogno is your primary key, so you need to identify the contents in the text file too.
Need to have a structure in your text file. How you separate the data in this file?
Why do you want to export the table into a file and use it to update the table? you can make online pages to make this update feature.

hi djjjozsi,

the structure is same in the text file and the table …
i tried both

$sql=“load data infile ‘upd_20090527c.txt’ into table nielupdate”;
$sql=“insert ignore into nielupdate select * from ‘upd_20090527c.txt’”;

No use …

any help how to do it …

Can you post a few lines from upd_20090527c.txt.

It should be a tab seperated file.

Also mysql_error() can give sone useful info.