简体   繁体   English

在网页中创建小程序

[英]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 . 我想要一个简单的网页,其中包含userName和password的字段。一旦用户输入正确的userName和password并按logIn按钮, 则它应该显示一个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. mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in即使输入正确的用户名和密码也不会打开applet。

Edit: After I changed to action="logindata.php" in the form, here's the logindata.php 编辑:在表单中更改为action =“ logindata.php”后,这是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. 引用这些WHERE stName='$un' AND stP='$pw'因为我们正在处理字符串,这就是为什么会出现布尔错误的原因。

Plus, use isset() around your executed code since you're using your entire code inside the same file. 另外,由于在同一文件中使用了整个代码,因此请在执行的代码周围使用isset()

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() 还要添加or die(mysqli_error($con))mysqli_query()


Footnotes: 脚注:

Your present code is open to SQL injection . 您当前的代码可以进行SQL注入

Use prepared statements , or PDO with prepared statements , they're much safer . 使用预处理语句或将PDO与预处理语句结合 使用起来更加安全


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. '../foldername/studentWeb.html'具体取决于文件的位置。


The <applet> tag is not supported in HTML5. HTML5不支持<applet>标记。 Use the <object> tag instead if you are having problems with your present code. 如果当前代码有问题,请使用<object>标记。 Yet, applets usually have the .class extension. 但是,小程序通常具有.class扩展名。

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


Edit #2: 编辑#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. 由于失败, mysqli_query()返回false Use $query="SELECT * FROM studentinfo WHERE stName=" . $un . " AND stP= " . $pw; 使用$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() 而且,这不是一个好方法,因为如果query失败,它将返回布尔值false ,这将再次为mysqli_num_rows()生成错误。

You may look into this for further information and this for better ways to query your database. 你可以看看到这个进一步的信息和这个更好的方式来查询数据库。

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

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