0

可能是愚蠢的问题,但是......我有一个 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";
}

?>
4

3 回答 3

0

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

$sql="UPDATE $tbl_name 
      SET name='TEST', 
          lastname='".$_POST['lastname']."', 
          email='".$_POST['email']."' 
      WHERE id=".$_POST['id'];
于 2013-10-22T13:18:07.703 回答
0

这是您在更新查询中需要的

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

每当form向 中提到的文件发布/提交值时,该文件必须访问数组action中表单字段名称的值作为。等(如果used在标签中)$_POST$_POST['lastname'], $_POST['email']methodPOSTform

访问提交的值有三种方式:

  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
于 2013-10-22T13:21:08.843 回答
0

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

如果您不确定您通过哪种方法从表单传递值,您可以使用 $_REQUEST 方法。但尽量避免使用它,因为它不太安全。

于 2013-10-22T13:28:37.797 回答