Drupal DB : Have to move some data from one table to another by cheking the condition at remote server..!
-
12-06-2021 - |
質問
I am working with a portal, where there is content type. The front end developer who made this portal added a CCK field for file attachment & there is already File attachment field given by core. Now we won't go to discuss why did he do that as I also don't know.
Now I have to removed that CCK field & if I remove directly, I will loose all the data uploaded by this field as it will remove the table of it.
So now I am trying to replace the value from this CCK field table to default upload table & update the filepath in files table..so that it would come in view for attached files.
Because I am not very good at PHP or MySQL I am seeking some help from you guys..
Here is the basic code, which I have written...please suggest the correction & please tell me will I be able to connect remote server DB in doing it so -
<?php
mysql_connect("http://edlabs.in/connect:22","connect","");
mysql_select_db("connect");
for($i=0; $i<=1; $i++){
$fid_cck = array(mysql_query("SELECT field_upload_doc_fid FROM content_field_upload_doc WHERE field_upload_doc_list > 0"));
$fid_core = array(mysql_query("SELECT fid FROM upload"));
foreach($fid_cck as $fid_cck_value){
foreach($fid_core as $fid_core_value)
{
if($fid_cck_value != $fid_core_value){
$file_name = mysql_query("SELECT filename FROM files WHERE fid = $fid_cck");
$nid_cck = mysql_query("SELECT nid FROM field_upload_doc_fid WHERE fid = $fid_cck");
$vid_cck = mysql_query("SELECT vid FROM field_upload_doc_fid WHERE fid = $fid_cck");
mysql_query("INSERT INTO upload VALUES('$fid_cck', '$nid_cck', '$vid_cck', '$file_name', 1, 0)");
$filepath = array(mysql_query("SELECT filepath FROM files WHERE fid = $fid_cck"));
$new_filepath = str_replace("/background_docs", "", $filepath);
mysql_query("UPDATE files SET filepath=$new_filepath WHERE fid = $fid_cck");
}
}
}
}
?>
解決
I have solved this problem by following these steps-
- Created a DB with the same name as my portal at my local phpmyadmin.
- Exported three table named content_field_upload_doc, files & upload from DB of my portal.
- Wrote a custom PHP code with some MySQL query to make query and find DIFF of those files which are in content_field_upload_doc but not in upload table & picked the name of that from table files.(I am giving the code below).
- Ran the code on my wamp and echoed the INSERT query.
- Copied all the query & executed that with SQL on my phpmyadmin at portal DB.
- Copied all the files from previous used folder inside public folder & pasted that to default public folder.
Removed the CCK field from content type...and it's DONE..!
die('Could not connect: ' . mysql_error());
} else{
} ?>mysql_select_db("connect"); $rs_fudl = mysql_query("SELECT vid, nid, field_upload_doc_fid FROM content_field_upload_doc WHERE field_upload_doc_list > '0'"); while($rs_fudl_row = mysql_fetch_array($rs_fudl)) { $fid_fudl = $rs_fudl_row['field_upload_doc_fid']; $rs_file = mysql_query("SELECT filename,filepath FROM files WHERE fid = '$fid_fudl'"); //echo mysql_num_rows($rs_file)."<br />"; while($rs_file_row = mysql_fetch_array($rs_file)){ $rs_upload = mysql_query("SELECT * FROM upload where fid='".$fid_fudl."'"); if ( mysql_num_rows($rs_upload) == 0) { /* $new_filepath = str_replace("/background_docs", "", $rs_file_row['filepath']); $filepath = $new_filepath; */ $filename = $rs_file_row['filename']; $vid_fudl = $rs_fudl_row['vid']; echo "<br />"; $nid_fudl = $rs_fudl_row['nid']; echo "<br />"; echo "INSERT INTO upload VALUES ('$fid_fudl','$nid_fudl', '$vid_fudl', '$filename','1', '0');" ; echo "<br />"; // echo "UPDATE files SET filepath= '$filepath' WHERE fid = '$fid_fudl';"; } } } mysql_close($con);