0

我有 2 个文件,1)表格 2)更新文件

这是更新表单 update.php:

<?php

$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="datasename"; // Database name
$tbl_name="data"; // 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);
?>


<div align="center"><img src="http://www.multihunters.co.uk/images/tf_ideal/blue/misc/logo.png" width="400" height="125">

</div>
<div align="center">
  <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>Citizen ID</strong></td>
              <td align="center"><strong>Citizen Name</strong></td>
              <td align="center"><strong>Citizenship</strong></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td align="center">
                <input name="citid" type="text" id="citid" value="<? echo $rows['citid']; ?>">
              </td>
              <td align="center">
                <input name="citname" type="text" id="citname" value="<? echo $rows['citname']; ?>" size="15">
              </td>
              <td>
                <input name="citizenship" type="text" id="citizenship" value="<? echo $rows['citizenship']; ?>" size="15">
              </td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>
                <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
              </td>
              <td align="center">
                <input type="submit" name="Submit" value="Submit">
              </td>
              <td>&nbsp;</td>
            </tr>
          </table>
        </td>
      </form>
    </tr>
  </table>
</div>


<?php
// close connection
mysql_close();

?>

这是我的操作表单 update_ac.php:

$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
$tbl_name="data"; // 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 citid='$citid', citname='$citname', citizenship='$citizenship' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='http://www.multihunters.co.uk/citizens_permlist.php'>View result</a>";
}
else
{
    echo "ERROR";
}

消息成功出现,但是当我检查数据是否已更新时,没有任何变化,我做错了什么?

4

2 回答 2

2

您没有定义$citid$citname和。您必须从中获取它,对其进行转义,然后将其放入查询中。$citizenship$id$_POST

另外,请停止使用mysql_*API。使用mysqli或 PDO 扩展。

于 2013-05-11T10:25:55.973 回答
0

只需将代码更改为

$citid = mysql_real_escape_string($_POST['citid']);
$citname = mysql_real_escape_string($_POST['citname']);
$citizenship = mysql_real_escape_string($_POST['citizenship']);

$sql="UPDATE $tbl_name SET citid='$citid', citname='$citname', citizenship='$citizenship' WHERE id='$id'";
$result=mysql_query($sql);

笔记

自 PHP 5.5.0起,以 开头的 PHP 函数mysql_已被弃用。如果您有能力这样做,请考虑更新您的代码以改用MySQLiPDO扩展。

于 2013-05-11T10:31:03.840 回答