简体   繁体   中英

Creating a applet in a web page

I want to have a simple web page with fields for userName and password.Once a user enters the correct userName and password and press logIn button then it should display an applet . Here's my code.

<?php  
$un=$_POST["username"];  
$pw=$_POST["password"];  
$log=$_POST["Login"];  
$con=mysqli_connect("localhost","root","","");  
if(mysqli_connect_errno($con))  
    {  
    echo "Failed to connect".mysqli_connect_error();  
    }  
mysqli_select_db($con,student);  
$query="SELECT * FROM studentinfo WHERE stName=$un AND stP=$pw";  

$result=mysqli_query($con,$query);  
$num_rows = mysqli_num_rows($result);  
if($log){  
    if($num_rows==1){  
    $isLogged=true;  
    }  
    else{  
    echo "Error log In.Invalid username or password";     
    $isLogged=false;  
    }  
}  
?>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>User LogIn</title>  
</head>  

<body>  
<form method="post">  
            <label>Username<input name="username" type="text" /></label> <label>Password<input name="password" type="password" /></label>  
            <input name="cmd" type="submit" value="Login" />  
        </form>  
<?php if($isLogged) {?>  
<applet code="studentWeb.html" width="32" height="32" alt="Couldn't launch applet" title="Student Details">  
</applet>  
<?php }?>  
</body>  
</html>  

Right now it gives an error as

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in and it doesn't open up a applet even when correct username and password has been entered.

Edit: After I changed to action="logindata.php" in the form, here's the logindata.php

<?php
$connect=mysqli_connect("localhost","root","");
if(mysqli_connect_errno($connect))
{
    echo "Failed to connect".mysqli_connect_error();
}
mysqli_select_db($connect,"student") or die("couldn't connect to db");
$un=$_POST["username"];
$pw=$_POST["Password"];

$sql="SELECT * FROM studentinfo WHERE stUserName='$un' AND stPassword='pw'";

$query=mysqli_query($connect,$sql) or die("couldn't find values");

if($query){
    include_once("studentWeb.html");
}
else{
echo ("Invalid username or password");

}


?>  

Why does this allow incorrect password and username ?

Quote these WHERE stName='$un' AND stP='$pw' since we're dealing with strings, which is why you are getting a boolean error.

Plus, use isset() around your executed code since you're using your entire code inside the same file.

You stand at getting an undefined index warning upon page entry.

Using error reporting will give you that:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Also add or die(mysqli_error($con)) to mysqli_query()


Footnotes:

Your present code is open to SQL injection .

Use prepared statements , or PDO with prepared statements , they're much safer .


Edit:

If you wish to include a file (show contents of), do the following, which I am under the impression you wish to do:

<?php if($isLogged) { 

    include 'studentWeb.html';
}
?>

This is to be replaced by your present code:

<?php if($isLogged) {?>  
<applet code="studentWeb.html" width="32" height="32" alt="Couldn't launch applet" title="Student Details">  
</applet>  
<?php }?>

You can also try:

<?php $file = file_get_contents('studentWeb.html', true);
  echo $file;
?>

or '../foldername/studentWeb.html' depending on where the file is located.


The <applet> tag is not supported in HTML5. Use the <object> tag instead if you are having problems with your present code. Yet, applets usually have the .class extension.

Consult http://www.tutorialspoint.com/html/html_applet_tag.htm


Edit #2:

As per your edit, change:

if($query){
    include_once("studentWeb.html");
}
else{
echo ("Invalid username or password");

}

to:

$numrows = mysqli_num_rows($query);

if($numrows > 0){
   include_once("studentWeb.html");
}

mysqli_query() is returning false because of failure. Use $query="SELECT * FROM studentinfo WHERE stName=" . $un . " AND stP= " . $pw; $query="SELECT * FROM studentinfo WHERE stName=" . $un . " AND stP= " . $pw; Moreover, this is not a good way because if your query fails, it will return boolean false which will again generate error for mysqli_num_rows()

You may look into this for further information and this for better ways to query your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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