PHP MySQL 简单更新:无法读取值


php mysql simple update: cannot read values

可能是愚蠢的问题,但是...我有一个PHP和MySQL的样本,它不起作用...有 3 个 PHP 文件:

  1. list_records.php
  2. 更新.php
  3. update_ac.php

问题是第三个不会从第二个读取变量。我做错了什么?

这是我的文件/代码:

list_records.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // 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>Name</strong></td>
<td align="center"><strong>Lastname</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><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>

<td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

更新.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // 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
$id=$_GET['id'];
// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$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="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection 
mysql_close();
?>

update_ac.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // 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 name='TEST', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>

将值插入或更新到数据库中之前,您需要从客户端获取服务器端的值。为此,我们在PHP中有超全局变量,如$_GET,$_POST,$_REQUEST等。因此,请在update_ac.php中包含以下内容: $lastname=$_POST[lastname]; $email=$_POST[email]; $id=$_POST[id];'

如果您不确定 U 通过哪个方法传递表单中的值,则可以使用 $_REQUEST 方法。但请尽量避免使用此方法,因为它不太安全。

您需要从update_ac.php文件中的POST中获取它们,例如

$sql="UPDATE $tbl_name 
      SET name='TEST', 
          lastname='".$_POST['lastname']."', 
          email='".$_POST['email']."' 
      WHERE id=".$_POST['id'];
这是

更新查询中需要的

......lastname='".$_POST['lastname']."', email='".$_POST['email']."' WHERE id=".$_POST['id']

每当formaction中提到的文件发布/提交值时,该文件必须访问数组中表单字段名称的值$_POST$_POST['lastname'], $_POST['email']等(如果使用的method POSTform标签中)

有三种方法可以访问提交的值:

  1. 获取 http://php.net/manual/en/reserved.variables.get.php
  2. 发布 http://php.net/manual/en/reserved.variables.post.php
  3. 请求 http://php.net/manual/en/reserved.variables.request.php