Jump to content

Move data between tables!!!


jomagi

Recommended Posts

Hi there!

 

Can't make it work, I want to move a record from table1 to table2 by selecting a field name from table1 to match name of table2.

The 2 tables are the same structure minus auto-increment in table2.

Can anyone help me? Thanks.

 

<?php require_once('Connections/destaques.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
} 

if ((isset($_GET['id'])) && ($_GET['id'] != "")) {

  $query = sprintf("SELECT * FROM destaques WHERE id=%s",
                       GetSQLValueString($_GET['id'], "int"));
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  $editname = $row["editora"];
  
  $moveSQL = "INSERT INTO '$editname' VALUES ('','$row["id"]','$row["editora"]','$row["artista"]','$row["disco"]','$row["ref"]','$row["imagem"]','$row["estilo"]')";
  $action = mysql_query($moveSQL);
      
  $deleteSQL = sprintf("DELETE FROM destaques WHERE id=%s",
                       GetSQLValueString($_GET['id'], "int"));

  mysql_select_db($database_destaques, $destaques);
  $Result1 = mysql_query($deleteSQL, $destaques) or die(mysql_error());

  $deleteGoTo = "admin_destaques.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}
?>

Link to comment
https://forums.phpfreaks.com/topic/142823-move-data-between-tables/
Share on other sites

theres no query that able to move table

CREATE TABLE `bintaro_medrec`.`terkini` (
`ID` int( 5 ) NOT NULL AUTO_INCREMENT ,
`KATEGORI` int( 1 ) default NULL ,
`HARI` varchar( 6 ) default NULL ,
`TANGGAL` varchar( 20 ) default NULL ,
`KOTA` varchar( 30 ) default NULL ,
`JUDUL` varchar( 200 ) default NULL ,
`ABSTRAK` varchar( 250 ) default NULL ,
`IMAGE1` varchar( 50 ) default NULL ,
`IMAGE2` varchar( 50 ) default NULL ,
`EXPIRE` int( 1 ) NOT NULL default '0',
`CONTENT` text,
PRIMARY KEY ( `ID` ) ,
UNIQUE KEY `id` ( `ID` ) ,
KEY `id_2` ( `ID` ) ,
KEY `expire` ( `EXPIRE` ) ,
KEY `EXPIRE_2` ( `EXPIRE` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =99;

INSERT INTO `bintaro_medrec`.`terkini`
SELECT *
FROM `bintaro_backup`.`terkini` ;

DROP TABLE `bintaro_backup`.`terkini` ;

try copy this script

1. copy all data from table1 to table2

2. drop table 1.

3. check.. if any of the process error

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.