我如何禁用或影响 php 中的变量为 null

我有 3 个网页,一个用于登录,一个用于注册,最后一个是主页(索引)。我有 2 个来自登录的变量和 5 个来自注册的变量,所有变量都被发送到主页,因此当我在登录变量中注册时没有值,因此会出现错误“注意未定义的索引” ,当我注册时,也会发生同样的情况,但错误显示了登录的变量。我在谷歌上进行了很多搜索,发现了一个名为 unset 的函数来禁用变量,但没有任何反应。


// Sous WAMP

$bdd = new PDO('mysql:host=localhost;dbname=test exbook;charset=utf8', 'root', '');

$reponse = $bdd->query('SELECT * FROM registration');

$nom = $_POST['nom'];

$birthday = $_POST['birthday'];

$genre = $_POST['genre'];

$email = $_POST['email'];

$password = password_hash($_POST['password'], PASSWORD_DEFAULT);


if (isset($nom , $birthday , $genre , $email, $password )) {

    unset($nom);

    unset($birthday);

    unset($genre);

    unset($email);

    unset($password);

} else {

    $req = $bdd->prepare('INSERT INTO registration(`Nom`, `Date de naissance`, `Genre`, `Email`, `Password`) VALUES(?,?,?,?,?)');

    $req->execute(array($nom, $birthday, $genre, $email, $password));

}


while ($donnees = $reponse->fetch()) {

    echo $donnees['Nom'];

}


$bdd = new PDO('mysql:host=localhost;dbname=test exbook;charset=utf8', 'root', '');

$emailsignup = $_POST['emailsignup'];

if (isset($emailsignup)) {

    unset($emailsignup);

} else{

    $bdd1 = $bdd->query('SELECT * FROM registration WHERE $emailsignup');

}


感谢您阅读本文并尽力提供帮助。祝你今天过得愉快 :)


斯蒂芬大帝
浏览 121回答 1
1回答

倚天杖

未定义索引错误来自 $_POST 变量。当您尝试emailsignup参数nom, birthday...时,$_POST 数组中不存在。你可以这样做:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Sous WAMP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bdd = new PDO('mysql:host=localhost;dbname=test exbook;charset=utf8', 'root', '');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $reponse = $bdd ->query('SELECT * FROM registration');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hasnom=isset($_POST['nom']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hasbirthday=isset($_POST['birthday']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hasgenre=isset($_POST['genre']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hasemail=isset($_POST['email']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $haspass = $isset($_POST['pass']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($hasnom && $hasbirthday && $hasgenre && hasemail && haspass) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nom=$_POST['nom'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $birthday=$_POST['birthday'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $genre=$_POST['genre'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $email=$_POST['email'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $password=password_hash($_POST['password'], PASSWORD_DEFAULT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $req = $bdd ->prepare('INSERT INTO registration(`Nom`, `Date de naissance`, `Genre`, `Email`, `Password`) VALUES(?,?,?,?,?)');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $req->execute(array($nom,$birthday,$genre,$email,$password));&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ($donnees = $reponse->fetch())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $donnees['Nom'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bdd = new PDO('mysql:host=localhost;dbname=test exbook;charset=utf8', 'root', '');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hasemailsignup=isset($_POST['emailsignup']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($hasemailsignup) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $emailsignup = $_POST['emailsignup'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bdd1=$bdd ->query('SELECT * FROM registration WHERE $emailsignup');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP