[英]Creating a applet in a web page
我想要一個簡單的網頁,其中包含userName和password的字段。一旦用戶輸入正確的userName和password並按logIn按鈕, 則它應該顯示一個applet 。 這是我的代碼。
<?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>
現在它給出了一個錯誤
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
即使輸入正確的用戶名和密碼也不會打開applet。
編輯:在表單中更改為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");
}
?>
為什么這允許使用錯誤的密碼和用戶名 ?
引用這些WHERE stName='$un' AND stP='$pw'
因為我們正在處理字符串,這就是為什么會出現布爾錯誤的原因。
另外,由於在同一文件中使用了整個代碼,因此請在執行的代碼周圍使用isset()
。
您站在頁面進入時收到未定義的索引警告。
使用錯誤報告將為您提供:
將錯誤報告添加到文件頂部,這將有助於發現錯誤。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:錯誤報告僅應在登台進行,而不應在生產過程中進行。
還要添加or die(mysqli_error($con))
到mysqli_query()
腳注:
您當前的代碼可以進行SQL注入 。
使用預處理語句或將PDO與預處理語句結合 使用起來更加安全 。
編輯:
如果您希望包括一個文件(顯示其內容),請執行以下操作,我對您的印象是:
<?php if($isLogged) {
include 'studentWeb.html';
}
?>
這將由您當前的代碼代替:
<?php if($isLogged) {?>
<applet code="studentWeb.html" width="32" height="32" alt="Couldn't launch applet" title="Student Details">
</applet>
<?php }?>
您也可以嘗試:
<?php $file = file_get_contents('studentWeb.html', true);
echo $file;
?>
或'../foldername/studentWeb.html'
具體取決於文件的位置。
HTML5不支持<applet>
標記。 如果當前代碼有問題,請使用<object>
標記。 但是,小程序通常具有.class
擴展名。
咨詢http://www.tutorialspoint.com/html/html_applet_tag.htm
編輯#2:
根據您的修改,更改:
if($query){
include_once("studentWeb.html");
}
else{
echo ("Invalid username or password");
}
至:
$numrows = mysqli_num_rows($query);
if($numrows > 0){
include_once("studentWeb.html");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.