我正在尝试创建一个脚本,将数据从一个数据库中的表更新到另一个数据库中的表。我是白手起家,我的能力非常有限。我确信有更好的方法可以做到这一点,但它可能超出我的能力范围。无论如何,这就是我正在尝试的方式。
第一个文档从我想从中获取信息的表中选择数据。它包含一个链接,允许我选择要复制的行。该链接基本上创建了一个包含我要复制到第二个数据库中的信息的表单。这个想法是,当我按下提交时,它会将信息发送到第二个数据库并更新字段。
我遇到的问题是,即使它说它已经成功执行了查询,它根本不会更新第二个数据库。
谁能明白为什么这不起作用?
这些是我正在使用的脚本:
测试显示.php
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db1"; // Database name
$tbl_name="table1"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Uid</strong></td>
<td align="center"><strong>User Name</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['uid']; ?></td>
<td><? echo $rows['user']; ?></td>
<td><? echo $rows['password']; ?></td>
<td><? echo $rows['email']; ?></td>
// link to update.php and send value of id
<td align="center"><a href="http://soundsurgery.co.uk/main/TEST5/TEST-EDIT.php?uid=<? echo $rows['uid']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
测试编辑.php
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db1"; // Database name
$tbl_name="table1"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$uid=$_GET['uid'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE uid='$uid'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="TEST-EXEC.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Uid</strong></td>
<td align="center"><strong>User Name</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="uid" type="text" id="uid" value="<? echo $rows['uid']; ?>">
</td>
<td align="center">
<input name="user" type="text" id="user" value="<? echo $rows['user']; ?>">
</td>
<td align="center">
<input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="uid" type="hidden" id="uid" value="<? echo $rows['uid']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
测试执行.php
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db2"; // Database name
$tbl_name="table2"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET uname='$user', password='$password', email='$email' WHERE uid='$uid'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='TEST-DISPLAY2.php'>View result</a>";
}
else {
echo "ERROR";
}
?><!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
TEST-DISPLAY2.php 与 TEST-DISPLAY.php 相同,不同之处在于它显示第二个表的结果。