我有关于php& amp的麻烦MySQL的。我要从DB1的表中检索所有记录,然后我必须再次将它们插入到DB2的表中。
<?php
require_once 'includes/config.php';
include 'includes/header.php';
if(isset($_POST['go'])){
$query = mysql_query("SELECT id,username,password FROM $db_database1.account")
or die(mysql_error());
echo "Record ".mysql_num_rows($query)." retrieve";
while($result_row = mysql_fetch_array($query, MYSQL_ASSOC)){
$account_ID = $result_row['id'];
$username = $result_row['username'];
$password = $result_row['password'];
$query = mysql_query("INSERT INTO $db_database2.account(uid,username,password) VALUES('$account_ID','$username','$password')")
or die(mysql_error());
$selectId = mysql_insert_id();
}
}
mysql_close($conn);
?>
<div class="wrapper">
<div class="content">
<form method="post" action="<?PHP $_SERVER['PHP_SELF'];?>">
<input type="submit" name="go" value="Go" />
</form>
</div>
<?php include 'includes/footer.php';?>
根据此代码,只插入了一条记录。如何插入所有检索到的记录?
答案 0 :(得分:1)
要将记录插入另一个表,您需要一个查询,从没有PHP的mysql控制台运行:
INSERT INTO db_database2.account SELECT id,username,password FROM db_database1.account
您的代码备注
$_SERVER['PHP_SELF']
没有用处。只是将表格动作留空。 echo
此变量UPDATE account SET password = md5(concat(id,username,password));
在此类操作之前始终有数据库备份