0

您好我在这里遇到了一些问题我创建了一个脚本来更新用户帐户详细信息但是当填写表格并单击提交按钮时没有出现错误但同时表格中没有进行任何更改

这只是一个虚拟应用程序,所以一切都保持基本

     <?php


session_start();


    include('connect_mysql.php');



        if(isset($_POST['update']))
        {

            $usernameNew = stripslashes(mysql_real_escape_string($_POST["username"]));
            $passwordNew = stripslashes(mysql_real_escape_string($_POST["password"]));
            $first_nameNew = stripslashes(mysql_real_escape_string($_POST["first_name"]));
            $last_nameNew = stripslashes(mysql_real_escape_string($_POST["last_name"]));
            $emailNew = stripslashes(mysql_real_escape_string($_POST["email"]));



            $user_id = $_SESSION['user_id'];
            $editQuery = mysql_query("UPDATE users SET username='$usernameNew', password='$passwordNew', first_name='$first_nameNew', last_name='$last_nameNew' , email='$emailNew' WHERE user_id='$user_id'");


            if(!$editQuery)
            {
                echo mysql_error($editQuery);
                die($editQuery);
            }
        }

?>


<html>
<head>

<title>Edit Account</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />

</head>
<body>
<div id="wrapper">
<header><h1>E-Shop</h1></header>

    <article>
        <h1>Welcome</h1>
            <h1>Edit Account</h1>

<div id="login">
    <ul id="login">
    <form method="post" name="editAccount" action="userEditAccount.php"  >
        <fieldset>  
            <legend>Fill in the form</legend>
                <label>Select Username : <input type="text" name="username" /></label>
                <label>Password : <input type="password" name="password" /></label>
                <label>Enter First Name : <input type="text" name="first_name" /></label>
                <label>Enter Last Name : <input type="text" name="last_name" /></label>
                <label>Enter E-mail Address: <input type="text" name="email" /></label>
        </fieldset>
        <br />


                <input type="submit" value="Edit Account" class="button">
<input type="hidden" name="update" value="update">


    </form>

</div>
    <form action="userhome.php" method="post">
    <div id="login">
        <ul id="login">
            <li>
                <input type="submit" value="back" onclick="index.php" class="button">   
            </li>
        </ul>
    </div>      
</article>

<aside>
</aside>

<div id="footer">Text</div>
</div>

</body>
</html>

抱歉由于某种原因我忘记复制这部分faceslap

登录.php:

<?php
session_start();

    require('connect_mysql.php');

     if($_SERVER['REQUEST_METHOD'] == 'POST')
     {

        $username = $_POST["username"];
        $password = $_POST["password"];
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysql_real_escape_string($username);
        $password = mysql_real_escape_string($password);

        $query = mysql_query("SELECT * FROM users WHERE Username='$username' AND Password='$password'");
        $numrow = mysql_num_rows($query);

    if($username && $password){



    $query = mysql_query("SELECT * FROM  users WHERE username='$username'");
    $numrow = mysql_num_rows($query);

    if($numrow !=0){

        while($row = mysql_fetch_assoc($query)){

            $dbusername = $row['username'];
            $dbpassword = $row['password'];         
        }

        if($username == $dbusername && $password == $dbpassword ){

            $_SESSION['user_id'] = $user_id;
            header("Location: userhome.php");

        }
        else{
            echo "Incorect password";
        }

    }
    else{
        die("This user dosent exists");
    }
}
else{

    $reg = die("Please enter username and password");
}
}


?>
4

2 回答 2

1

您还没有session_start()在文件的开头调用,所以$username将是一个空字符串,并且 update 命令只会更新用户名是空字符串的行。

编辑:事实上,该代码甚至不会运行,因为你还没有调用session_start()isset($_SESSION['update'])将评估为假。

你的意思是写$_SESSION['update']吗?不应该这样$_POST['update']吗?

最后但并非最不重要的一点是,我个人会替换它:

<input name="update" type="submit" submit="submit" value="Edit Account" class="button">

有了这个:

<input type="submit" value="Edit Account" class="button">
<input type="hidden" name="update" value="update">

至少为了清楚起见。我不知道它是否仍然如此,但随着时间的推移,并非所有浏览器都提交了提交按钮的名称/值。

于 2013-03-02T06:13:43.997 回答
0

先生,从上面给出的代码中,我认为您的 login.php 中有错误

$_SESSION['user_id'] = $user_id;

您没有为 $user_id 分配值,这就是为什么将空白值设置为 $_SESSION['user_id']。

  <?php
session_start();

    require('connect_mysql.php');

     if($_SERVER['REQUEST_METHOD'] == 'POST')
     {

        $username = $_POST["username"];
        $password = $_POST["password"];
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysql_real_escape_string($username);
        $password = mysql_real_escape_string($password);

        $query = mysql_query("SELECT * FROM users WHERE Username='$username' AND Password='$password'");
        $numrow = mysql_num_rows($query);

    if($username && $password){



    $query = mysql_query("SELECT * FROM  users WHERE username='$username'");
    $numrow = mysql_num_rows($query);

    if($numrow !=0){
        $user_id = 0;  
        while($row = mysql_fetch_assoc($query)){

            $dbusername = $row['username'];
            $dbpassword = $row['password'];         
            $user_id = $row['user_id'];
        }

        if($username == $dbusername && $password == $dbpassword ){

            $_SESSION['user_id'] = $user_id;
            header("Location: userhome.php");

        }
        else{
            echo "Incorect password";
        }

    }
    else{
        die("This user dosent exists");
    }
}
else{

    $reg = die("Please enter username and password");
}
}


?>
于 2013-03-02T08:25:45.973 回答