更新数据库中的记录不起作用

时间:2012-04-09 14:13:51

标签: php

再次,我正在尝试研究php mysql,似乎我尝试了所有的事情来解决问题..但它似乎作为互联网的初学者代码没有帮助..我真的无法更新记录在数据库中。

<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("dbtry",$db);
$id = isset($_GET['id']) ? $_GET['id'] : null;
$submit = isset($_POST['submit']);
if ($id) {
if ($submit) {

        $result = mysql_query("select * from employees where id = " . mysql_real_escape_string($_GET['id']) );
        $row = mysql_num_rows($result);

if ($myrow != 0) {
mysql_query ("UPDATE employees SET firstname='$first',lastname='$last',address='$address',position='$position' WHERE id = '$id'");

}


echo "Thank you! Information updated.\n";
} else {
// query the DB
$result = mysql_query("SELECT * FROM `employees` WHERE `id` = " . mysql_real_escape_string($_GET['id']), $db);

$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
First name:<input type="Text" name="first" value="<?php echo $myrow["firstname"] ?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $myrow["lastname"] ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $myrow["address"]
?>"><br>
Position:<input type="Text" name="position" value="<?php echo $myrow["position"]
?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
} else {
// display list of employees
$result = mysql_query("SELECT * FROM employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n",  $_SERVER['PHP_SELF'], $myrow["id"],
$myrow["firstname"], $myrow["lastname"]);
}
}
?>
</body>
</html>

5 个答案:

答案 0 :(得分:1)

有两件事可能会导致您出现问题:首先,您尝试设置的值是尚未定义的变量。我假设您发现的begginers代码假设您启用了注册全局变量,您真的不想这样做!

第二个问题是,如果你确实启用了注册全局变量,那么数据就不会被清理,因此引号可能会发送更新错误。

请改为尝试:

$first = mysql_real_escape_string( $_POST['first'] );
$last = mysql_real_escape_string( $_POST['last'] );
$address= mysql_real_escape_string( $_POST['address'] );
$position = mysql_real_escape_string( $_POST['position'] );

mysql_query ("UPDATE employees SET firstname='$first',lastname='$last',address='$address',position='$position' WHERE id = '$id'");

这至少应该让你开始运行。我强烈建议你使用MySQLi库或PHP PDO,并考虑使用预准备语句来增加安全性。

答案 1 :(得分:0)

通常当我遇到这个问题时,这是因为自动提交已关闭,我忘了明确告知连接提交。

编辑:你试过这个:How can I implement commit/rollback for MySQL in PHP??根据您的设置,可以将InnoDB设置为自动提交,这意味着您需要明确告诉MySQL在完成后提交更新。

答案 2 :(得分:0)

mysql_query("UPDATE `employees` SET `firstname`='".$first."', `lastname`='".$last."',
`address`='".$address."', `position`='".$position."' WHERE `id` = '".$id".' ; ", $db) or 
die(mysql_error());

答案 3 :(得分:0)

我认为问题可能在于您与数据库的连接。 mysql_connect函数的第三个参数是密码。因此:

$db = mysql_connect("localhost", "root");

应该是:

$db = mysql_connect("localhost", "root", "yourPassword");

如果你发布了什么类型的错误,它也会有很大的帮助。

答案 4 :(得分:0)

您需要区分帖子和获取。请按照下面的工作示例。它会把你排除在外:D

<html>
<body>
<?php
$db = mysql_connect("localhost", "root","");
mysql_select_db("test",$db);

if($_SERVER['REQUEST_METHOD']=='POST')
{
    //SUBMIT FORM
    $id=isset($_POST['id'])?$_POST['id']:0;
    if ($id) {
        $result = mysql_query("select * from parameter where id = " . mysql_real_escape_string($id) );
        $rows = mysql_num_rows($result);
        if ($rows != 0) {
        mysql_query ("UPDATE parameter SET name='".$_POST['name']."',value='".$_POST['value']."' WHERE id = '".$id."'");
        echo "Thank you! Information updated.\n";
    }
    }
}

if($_SERVER['REQUEST_METHOD']=='GET')
{
    //SELECT WHERE ID=GER VAR AND DISPLAY   
    $id = isset($_GET['id']) ? $_GET['id'] :0;// 
    if ($id) {
    // query the DB
    $result = mysql_query("SELECT * FROM parameter WHERE `id` = " . mysql_real_escape_string($_GET['id']), $db);
    $myrow = mysql_fetch_array($result);
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
    First name:<input type="Text" name="name" value="<?php echo $myrow["name"] ?>"><br>
    Last name:<input type="Text" name="value" value="<?php echo $myrow["value"] ?>"><br>
    <input type="Submit" name="submit" value="Enter information">
    </form>
    <?php
    }
    else {
    // display list of employees
    $result = mysql_query("SELECT * FROM parameter",$db);
    while ($myrow = mysql_fetch_array($result)) {
    echo "<a href='".$_SERVER['PHP_SELF']."?id=".$myrow['id']."'>".$myrow['name'].": ".$myrow['value']."</a><br>";
}
}
}
?>
</body>
</html>