0

我几乎没有编程经验并尝试了第一个项目,我有点卡在如何更新数据库上,所以我点击编辑,正确的记录被加载到编辑屏幕 update.php

当我点击更新时,我从updated.php收到消息说数据库已更新,但数据库没有更新,当我显示记录时它们与更新前相同,在此先感谢您的帮助.

以下代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Form Edit Data</title>
</head>

<body>
    <table border=1>
    <tr>
        <td align=center>Form Edit Employees Data</td>
    </tr>
    <tr>
    <td>
        <table>
        <?
        $user_name = "";
        $password = "";
        $database = "";
        $server = "localhost";

        mysql_connect($server, $user_name, $password);
        $db_found = mysql_select_db($database);
        $id = $_GET['id'];
        $order = "SELECT * FROM MY_ID where ID = ' " .$id . " ' ";
        $result = mysql_query($order);
        $row = mysql_fetch_array($result);
        ?>
        <form method="post" action="edit_data.php"?id=<?= $id ?>>
            <input type="text" name="id" value="<? echo "$row[ID]"?>">
            <tr>        
                <td>First Name</td>
                <td>
                    <input type="text" name="FirsName" size="20" value="<? echo "$row[FirstName]"?>">
                </td>
            </tr>
            <tr>
                <td>Sur Name</td>
                <td>
                    <input type="text" name="SurName" size="40" value="<? echo "$row[SurName]"?>">
                </td>
            </tr>
            <tr>
                <td>Address</td>
                <td>
                    <input type="text" name="Address" size="40" value="<? echo "$row[Address]"?>">
                </td>
            </tr>
            <tr>
                <td align="right">
                    <input type="submit" name="submit" value="submit">
                </td>
            </tr>
        </form>
        </table>

    </td>
    </tr>
    </table>
</body>
</html>

这是另一个文件

<?php
$user_name = "";
$password = "";
$database = "";
$server = "";

mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database);

$id = $_REQUEST['ID'];
$FirstName = trim(mysql_real_escape_string($_POST["FirstName"]));
$SurName = trim(mysql_real_escape_string($_POST["SurName"]));
$Address = trim(mysql_real_escape_string($_POST["Address"]));

$sql = "UPDATE MY_ID SET FirstName='$FirstName',SurName='$SurName',Address='$Address' WHERE ID='$id'";
$result=mysql_query($sql);


if ($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='edit.php'>View result</a>";
}
else {
    echo "ERROR";
}

?>
4

3 回答 3

2

看起来你忘记了双引号和句号。你应该把它写成:'".$example."'

$sql = "UPDATE MY_ID SET FirstName='".$FirstName."',SurName='".$SurName."',Address='".$Address.:' WHERE ID='".$id."'";
于 2013-03-28T00:49:45.863 回答
0

这是因为您的表单方法是POST,并且您正在尝试GETID。可能 ID 返回 null。我的建议是在你的表单中放置一个隐藏的输入name="ID",然后在你发布的页面中阅读它$_POST["ID"];

于 2013-03-28T00:54:17.287 回答
0

是的,答案正如曼苏尔所说。您不应该对变量使用单一配额。

因此,编写如下代码是一种不好的做法:

<input type="text" value="<?php echo "$row[name]"; ?>">

它应该是

<input type="text" value="<?php echo $row['name']; ?>">

很清楚,并且在插入或更新记录时,您应该编写如下:

$sql = "UPDATE MY_ID SET FirstName='" . $FirstName . "',
                         SurName='" . $SurName . "',
                         Address='" . $Address . "' 
         WHERE ID='" . $id . "'";
mysql_query($sql);
于 2013-03-28T01:16:31.567 回答