dqroc48068 2016-01-15 11:27
浏览 143
已采纳

注意:未定义的变量:[value] [duplicate]

I have searched on stackoverflow about the undifined variable, and I couldn't find anything that solved my problem.

this is my PHP code:

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

    $username =mysql_real_escape_string( $_POST['naam']);
    $password =md5( $_POST['wacht']);

    $check_user = "select * from users where gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";

    $run =mysql_query($check_user);

    if(mysql_num_rows($run)>0){

    $_SESSION['naam']=$gebruikersnaam;

        echo "<script>window.open('welkom.php','_self')</script>";
    }

This is my HTML code:

<div id=content>
    <form method='POST' action='login.php'>
        Gebruikersnaam:<br>
        <input type='text' name='naam' />
        <br>
        <br>
        Wachtwoord:<br>
        <input type='password' name='wacht' />
        <br>
        <br>
        <input type='submit' name='login' value='Inloggen' />
    </form>
    <br>
    Niet geregistreerd? <a href='registratie.php'>Klik hier.</a>
</div>  

I can't see where my problem is, it says I have a undefined variable on this line: $check_user = "select * from users where gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";

The names I use are from my database so I wouldn't make a mistake with the names.

Could you people help me? It would be a real time saver because I can't find the problem.

</div>
  • 写回答

2条回答 默认 最新

  • dongqiuge5435 2016-01-15 11:43
    关注

    You do not check if the following variables are set before you assign them.

    $_POST['naam']
    $_POST['wacht']
    

    As you did with $_POST['login'] you can use isset to check they exist before assignment.

    if (isset($_POST['naam'] && isset($_POST['wacht']) {
        // Do something...
    }
    

    In addition to this in the query you are using the variables $gebruikersnaam and $wachtwoord which you don't appear to be referencing anywhere else? So after some google translating I'm guessing that you intended for this bit of code:

    $username =mysql_real_escape_string( $_POST['naam']);
    $password =md5( $_POST['wacht']);
    

    To be the following:

    $gebruikersnaam = mysql_real_escape_string($_POST['naam']);
    $wachtwoord     = md5($_POST['wacht']);
    

    Hopefully that helps, just a bit of a side note though, I would really advise reading over http://www.phptherightway.com/ and getting familiar with some of the best practices for PHP.

    In your code I would attempt to refactor it and utilise password_hash() and mysqli_* as MD5() is not secure and the mysql_ extension has been removed in the latest version of PHP and was deprecated before that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败