简体   繁体   English

提交编辑表单后,id消失时,请在“编辑”表单中验证用户名

[英]Validate the username in Edit form while id is gone when the Edit form is submitted

require_once "../config.php";

#Editing user's Id
if(isset($_GET['id'])){
    $id = $_GET['id'];
    $queryT = "SELECT * FROM teachers WHERE t_id = $id";
    $results = mysqli_query($link, $queryT);
    $myRow = mysqli_fetch_array($results);
}

$userName = htmlspecialchars($_POST['username']);

if(isset($_POST['submit'])){
if(empty($userName)){
        $userNameErr = "<span>Username is required!.</span>";
    }elseif($rowcount == 1 AND $userName != $myRow['username']){
        $userNameErr = "<span>That username is taken!.</span>";
    }

it all went well but when i submit the edited form the previous Get id fades and $myRow['username'] becomes undefined variable 一切顺利,但是当我提交编辑的表单时,以前的“获取ID”消失,$ myRow ['username']变为未定义变量

You can test whether the index is set with if (isset($myRow['username'])) 您可以测试是否使用if(isset($ myRow ['username']))设置索引

And you should always declare a variable in the scope that you want to use it. 而且,您应该始终在要使用的范围内声明变量。 Php is not strict so that means you have to be. Php并不严格,因此您必须做到。

$myRow = [];

if(isset($_GET['id'])){
    $id = $_GET['id'];
    $queryT = "SELECT * FROM teachers WHERE t_id = $id";
    $results = mysqli_query($link, $queryT);
    $myRow = mysqli_fetch_array($results);
}

To preserve the get variable you can store it in a hidden field of your form and do something like 要保留get变量,可以将其存储在表单的隐藏字段中,并执行类似的操作

$id = (isset($_GET['id'])  
    ? $_GET['id'] 
    : isset($_POST['id']) 
        ? $_POST['id'] 
        : 0;
if($id !== 0){
    $queryT = "SELECT * FROM teachers WHERE t_id = $id";
    $results = mysqli_query($link, $queryT);
    $myRow = mysqli_fetch_array($results);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM