如何使用MySQL将数据从一个表复制到另一个表,但列数不同。 我只想在复制选定的行期间添加随机数。
我只想将选定的行复制到另一个表中,但是要在其中添加新代码。我尝试使用下面的代码,但它不会工作。我试试Copy from one column to another (different tables same database) mysql,但它也不会起作用。
的index.php
<?php
$connect = mysqli_connect("localhost", "root", "1234", "testng");
$query = "SELECT * FROM tbl_employee ORDER BY id desc";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Migrate</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:900px;">
<div class="table-responsive" id="employee_table">
<table class="table table-bordered">
<tr>
<th width="5%">ID</th>
<th width="25%">Name</th>
<th width="35%">Address</th>
<th width="10%">Gender</th>
<th width="20%">Designation</th>
<th width="5%">Age</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><?php echo $row["designation"]; ?></td>
<td><?php echo $row["age"]; ?></td>
<td><button type="button" class="btn btn-primary" onclick="migrate(<?php echo $row["id"]; ?>)">MIGRATE</button></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
<script>
function migrate(id) {
var mte_req = id;
$.post("migrate.php", {
mte_req: mte_req
},
function(data, status) {
//
}
);
}
</script>
migrate.php
<?php
$host = "localhost";
$user = "root";
$password = "1234";
$database = "testng";
$db = mysqli_connect($host, $user, $password,$database) or die("Could not connect to database");
if (isset($_POST['mte_req'])) {
$migratethis=$_POST['mte_req'];
function randomKey($length) {
$pool = array_merge(range(0,9), range('a', 'z'));
$key='';
for($i=0; $i < $length; $i++) {
$key .= $pool[mt_rand(0, count($pool) - 1)];
}
return $key;
}
$commit_tracer=randomKey(10);
$query="INSERT INTO new_employee (code, name,address,gender,designation,age) VALUES ('commit_tracer', SELECT name,address,gender,designation,age FROM tbl_employee WHERE id='$migratethis')";
if (!$result = mysqli_query($db,$query)) {
exit(mysqli_error());
}
}
?>
数据库转储文件
-- Dumping database structure for testng
CREATE DATABASE IF NOT EXISTS `testng` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `testng`;
-- Dumping structure for table testng.new_employee
CREATE TABLE IF NOT EXISTS `new_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(50) NOT NULL DEFAULT '0',
`name` varchar(50) NOT NULL,
`address` text NOT NULL,
`gender` varchar(10) NOT NULL,
`designation` varchar(100) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
-- Dumping data for table testng.new_employee: 0 rows
/*!40000 ALTER TABLE `new_employee` DISABLE KEYS */;
/*!40000 ALTER TABLE `new_employee` ENABLE KEYS */;
-- Dumping structure for table testng.tbl_employee
CREATE TABLE IF NOT EXISTS `tbl_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`address` text NOT NULL,
`gender` varchar(10) NOT NULL,
`designation` varchar(100) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=177 DEFAULT CHARSET=latin1;
-- Dumping data for table testng.tbl_employee: 4 rows
/*!40000 ALTER TABLE `tbl_employee` DISABLE KEYS */;
INSERT INTO `tbl_employee` (`id`, `name`, `address`, `gender`, `designation`, `age`) VALUES
(175, 'Jane Doe', 'Alabama', 'Female', 'Junior Analyst', 23),
(176, 'John Doe', 'Connecticut', 'Male', 'Associate Software Engineer', 21),
(174, 'Jane Ong', 'New York', 'Female', 'WebDev', 22),
(173, 'test Run', 'Mississipi', 'Male', 'WebDev', 21);
/*!40000 ALTER TABLE `tbl_employee` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;