【问题标题】:PHP/MySQL Table Update IssuePHP/MySQL 表更新问题
【发布时间】:2014-07-19 17:05:03
【问题描述】:

我有一个 MySQL 数据库设置,其中包含一堆用户名、密码和其他详细信息。我在网站上有一个“管理员”部分,我希望能够在需要时编辑用户信息。

到目前为止,我有一个名为president_admin.php 的页面(代码如下),它显示了一个包含用户名、用户名和密码的表格,以及一个更新用户的链接。我的表格工作正常,它显示信息和所有内容。更新链接,获取表中的用户 ID,然后将其发送到名为 StudentEdit.php 的页面(代码如下),然后检索用户的 ID,并用用户名、密码和名称填写三个文本字段就好了.有用。

所以我填写了要测试的字段,看看它是否能让我更新数据库中的变量,然后按提交进入一个名为 StudentUpdate.php 的页面(代码如下)。页面连接数据库,使用and if 显示,不更新则报错。它运行良好,并说它成功更新......但它确实没有。有人可以看看我的代码并帮助我。

我已经坚持了好几个小时了。请注意,这是一个代码片段,它实际上是安全的,并且页面上有更多内容。另外,我是一名青少年,正在尽我所能......对不起,如果这是对编码世界的审查。

president_admin.php

<?php
                    session_start(); 
                    $host="localhost"; // Host name 
                    $username=""; // Mysql username 
                    $password=""; // Mysql password 
                    $db_name=""; // Database name 
                    $tbl_name="members"; // Table name

                    // Connect to server and select databse.
                    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
                    mysql_select_db("$db_name")or die("cannot select DB");

                    $sql="SELECT * FROM $tbl_name";
                    $result=mysql_query($sql);                  
?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
                <tr>
                    <td>
                        <table width="100%" border="1" cellspacing="0" cellpadding="3">

                            <tr>
                            <td align="center"><strong>Name</strong></td>
                            <td align="center"><strong>Username</strong></td>
                            <td align="center"><strong>Password</strong></td>
                            <td align="center"><strong>Update</strong></td>
                            </tr>

                            <?php
                            while($rows=mysql_fetch_array($result)){
                            ?>

                            <tr>
                            <td><? echo $rows['name']; ?></td>
                            <td><? echo $rows['username']; ?></td>
                            <td><? echo $rows['password']; ?></td>

                            <td align="center"><a href="StudentEdit.php?id=<? echo $rows['id']; ?>">Update</a></td>
                            </tr>

                            <?php
                            }
                            ?>
                            <?php
mysql_close();
?>

                        </table>
                    </td>
                </tr>
            </table>

StudentEdit.php

<?php
                    session_start(); 
                    $host="localhost"; // Host name 
                    $username=""; // Mysql username 
                    $password=""; // Mysql password 
                    $db_name=""; // Database name 
                    $tbl_name="members"; // Table name

                    // Connect to server and select databse.
                    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);
?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
                <tr>
                <form name="form1" method="post" action="StudentUpdate.php">
                <td>
                <table width="100%" border="0" cellspacing="1" cellpadding="0">
                <tr>
                <td>&nbsp;</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>Name</strong></td>
                <td align="center"><strong>Username</strong></td>
                <td align="center"><strong>Password</strong></td>
                </tr>
                <tr>
                <td>&nbsp;</td>
                <td align="center">
                <input name="name" type="text" id="name" value="<? echo $rows['name']; ?>">
                </td>
                <td align="center">
                <input name="username" type="text" id="username" value="<? echo $rows['username']; ?>" size="15">
                </td>
                <td>
                <input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" 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><?php
// close connection 
mysql_close();
?>

StudentUpdate.php

<?php
                    session_start(); 
                    $host="localhost"; // Host name 
                    $username=""; // Mysql username 
                    $password=""; // Mysql password 
                    $db_name=""; // Database name 
                    $tbl_name="members"; // Table name

                    // Connect to server and select databse.
                    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 members SET name='$name', username='$username', password='$password' WHERE id='$id'";
                    $result=mysql_query($sql);

                    // if successfully updated. 
                    if($result){
                    echo "Successfully Updated";
                    echo "<BR>";
                    echo "<a href='president_admin.php'>Return to dashboard</a>";
                    }

                    else {
                    echo "Error, something went wrong.";
                    }
?>

【问题讨论】:

  • 对管理页面的无限制访问不是您应该保留的,也许是出于测试目的。但请记住,要添加这样的功能!您不希望可能的恶意/爱管闲事的学生获得对管理面板的访问权限
  • 这只是一个代码片段......它的限制哈哈
  • 不要在你的代码中使用 mysql_* 但 mysqli_*,不再推荐使用 mysql。
  • @user3626699 尝试 echo $sql 打印您的查询,即 $sql="UPDATE members SET name='$name', username='$username', password='$password' WHERE id=' $id'";它将帮助您了解您的查询出了什么问题,请在保存时加密您的密码
  • 在开发过程中将错误报告添加到文件顶部error_reporting(E_ALL); ini_set('display_errors', 1); 并使用var_dump(); 跟踪设置或未设置的内容。

标签: php mysql database html html-table


【解决方案1】:
add this to your studentupdate.php
if($_POST['Submit']))
{
$name=$_POST['name'];
$username=$_POST['username'];
$password=$_POST['password'];
$id=$_POST['id'];
//your script
}
and also change the variable name in connection script
$username=""; // Mysql username 
$password="";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多