search
HomeBackend DevelopmentPHP Tutorial修改密码后接收不了数据


最后的输出结果是   帐号或密码不能为空

在添加数据的时候是没有问题的,但是如果是修改密码的时候就出现问题了
用var_dump()打印的时候,还没有提交表单可以打印出用户名,提交后打印不出用户名




$id = (int) $input->get('id');
//如果编辑状态,取出用户名
if( $id>0 ){
    $sql = "SELECT * FROM adminuser WHERE id='{$id}' limit 1";
    $editUser = $db->get($sql);
    if( !$editUser ){
        echo "不存在帐号";exit;
    }
}
//添加功能和修改功能,都在这里
$do = $input->get('do');
if( $do == 'save' ){
    $username = $id>0 ? $esitUser['username'] :  trim($input->post('username'));
    $passwd = trim($input->post('passwd'));
        //不能为空
        if( empty($username) || empty($passwd) ){
            echo "帐号或密码不能为空";exit;
        }
        //用户名不能重复
        $sql = "SELECT * FROM adminuser WHERE username='{$username}' limit 1";
        $row = $db->get($sql);
        if( !$row ){
            //执行插入动作
            $sql = sprintf("INSERT INTO adminuser(username,passwd) value ('%s','%s')",$username,$passwd);
            $db->query($sql);
            header("location: adminuser.php");
            exit;
        }elseif( $id>0 ){
            $sql = sprintf("UPDATE adminuser set passwd='{$passwd}' WHERE id='{$id}' ");
            $db->query($sql);
            header("location: adminuser.php");
        }else{
            echo "用户名重复了";exit;
        }        
}





回复讨论(解决方案)

$id 是什么?
你在插入新用户时并没有取回相应的 id
那么 修改时用到的 id 是从哪里来的?

$id 是什么?
你在插入新用户时并没有取回相应的 id
那么 修改时用到的 id 是从哪里来的?


$id是客户机的cookie的name,在input类里面调用$__GET,来获取
$id是判断用户有没有登录的,只要登录就可以获取当前登陆的id

添加用户的时候只会跳回前一个管理页面,但还是现在管理者的id

action=".....";,,,修改的时候会获得当前登陆用户的id,一起提交

至少在你贴出的代码片段中,没有看到你的这个赋值过程

            $sql = sprintf("INSERT INTO adminuser(username,passwd) value ('%s','%s')",$username,$passwd);
            $db->query($sql);
            header("location: adminuser.php");
            exit;
然后就结束了,并没有获取 id

你说 在input类里面调用$__GET,来获取
并且 action=".....";,,,修改的时候会获得当前登陆用户的id,一起提交
显然你的表单是 get 方式的
那么:action 上的任何 url 参数都会因提交时的 url 重组而失效
也就是你根本就没有提交 id

//用户名不能重复
        $sql = "SELECT * FROM adminuser WHERE username='{$username}' limit 1";
        $row = $db->get($sql);

判断用户名是否重复,是在插入数据的时候判断的,if条件判断有问题

至少在你贴出的代码片段中,没有看到你的这个赋值过程

            $sql = sprintf("INSERT INTO adminuser(username,passwd) value ('%s','%s')",$username,$passwd);
            $db->query($sql);
            header("location: adminuser.php");
            exit;
然后就结束了,并没有获取 id

你说 在input类里面调用$__GET,来获取
并且 action=".....";,,,修改的时候会获得当前登陆用户的id,一起提交
显然你的表单是 get 方式的
那么:action 上的任何 url 参数都会因提交时的 url 重组而失效
也就是你根本就没有提交 id


大神,不好意思,我说得不好,也没有表达清楚
表单提交流程是这样的,用post方式来提交的,但是点了提交的时候就在action="?do=save&"加了这些元素
然后通过get的方式来获取网址上面的值来判断有没有点提交再执行添加数据,而是为了修改数据的时候有识别功能
我刚刚在调试的时候也有点眉目知道是点了提交接收不到id的值导致的,我再结合大神的分析去弄弄
有什么表达的不清楚的,请大神多多包涵,我刚学两个月左右,也有很多知识点不是很清楚的,也说不清楚,还望大神多多包涵

//用户名不能重复
        $sql = "SELECT * FROM adminuser WHERE username='{$username}' limit 1";
        $row = $db->get($sql);

判断用户名是否重复,是在插入数据的时候判断的,if条件判断有问题



        $sql = "SELECT * FROM adminuser WHERE username='{$username}' limit 1";
        $row = $db->get($sql);
        if( !$row ){
            //执行插入动作
            $sql = sprintf("INSERT INTO adminuser(username,passwd) value ('%s','%s')",$username,$passwd);
            $db->query($sql);
            header("location: adminuser.php");
            exit;
        }else{
            echo "用户名重复了";exit;
        }
大神,这段代码,我是这样想法的,首先查询数据库,然后通过数据库的返回的结果集取一行,用if来判断是否为真,如果为真,就说明数据库里面有一条这样的名字的数据了,弹出重复的提示,否则就添加一条数据
写的时候也有各种的问题存在,也希望大神能指出和给一些教导

解决问题了,是在from表单传递数据的时候只把值传递了过去,但在action里面没有声明这个值的名称,导致的在接收表单数据的时候接收不到而出现的问题

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Discover File Downloads in Laravel with Storage::downloadDiscover File Downloads in Laravel with Storage::downloadMar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment