0

我再次尝试研究 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>
4

5 回答 5

1

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

第二个问题是,如果您确实启用了注册全局变量,则不会对数据进行清理,因此引号可能会使更新出错。

试试这个:

$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,并考虑使用准备好的语句来增加安全性。

于 2012-04-09T14:27:22.917 回答
0

You need to differentiate post and get. Follow the working example below. It will sort you out :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>
于 2012-04-09T15:00:04.487 回答
0

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

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

应该:

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

如果您发布遇到的错误类型也会有很大帮助。

于 2012-04-09T14:29:14.270 回答
0
mysql_query("UPDATE `employees` SET `firstname`='".$first."', `lastname`='".$last."',
`address`='".$address."', `position`='".$position."' WHERE `id` = '".$id".' ; ", $db) or 
die(mysql_error());
于 2012-04-09T14:24:18.797 回答
0

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

编辑:您是否尝试过:如何在 PHP 中实现 MySQL 的提交/回滚?? 根据您的设置,可以将 InnoDB 设置为关闭自动提交,这意味着您需要明确告诉 MySQL 在完成后提交更新。

于 2012-04-09T14:16:42.183 回答