PHP表单:不更新mysql数据库

时间:2013-03-28 00:45:45

标签: php mysql

我几乎没有编程经验并尝试过第一个项目,我对如何更新数据库感到困惑,所以我点击编辑并将正确的记录加载到编辑屏幕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";
}

?>

3 个答案:

答案 0 :(得分:0)

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

$sql = "UPDATE MY_ID SET FirstName='".$FirstName."',SurName='".$SurName."',Address='".$Address.:' WHERE ID='".$id."'";

答案 1 :(得分:0)

这是因为您的表单方法是POST,而您正在尝试GET ID。 可能ID返回null。 我的建议是在表单中添加一个隐藏的输入,与name="ID"一样,然后在您发布的页面中将其作为$_POST["ID"];

阅读

答案 2 :(得分:0)

是的,答案是正如Mansours所说的那样。您不应该对您的变量使用单个配额。

因此,编写类似这样的代码是不好的做法:

<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);