在提交编辑表单时,如果id已消失,则在编辑表单中验证用户名

时间:2018-03-21 10:59:28

标签: php

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>";
    }

一切顺利但是当我提交编辑过的表格时,之前的Get ID会消失 和$ myRow [&#39;用户名&#39;]成为未定义的变量

1 个答案:

答案 0 :(得分:0)

您可以测试索引是否设置为if(isset($ myRow ['username']))

并且您应该始终在要使用它的范围内声明一个变量。 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);
}

要保留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);
}