Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php未从html表单接收值_Php_Html - Fatal编程技术网

Php未从html表单接收值

Php未从html表单接收值,php,html,Php,Html,在下面的代码中,php变量不接收来自html表单的数据。代码直接执行if-else语句而不等待输入 <?php if(mysql_connect("localhost","root","")==false) { die ("Connection Failed"); } mysql_select_db("fb"); $id=$_POST["email"]; $pwd=$_POST["password"]; $sql=mysql_query("SELECT* FROM admin WHER

在下面的代码中,php变量不接收来自html表单的数据。代码直接执行if-else语句而不等待输入

<?php
if(mysql_connect("localhost","root","")==false)
{
  die ("Connection Failed");
}
mysql_select_db("fb");
$id=$_POST["email"];
$pwd=$_POST["password"];
$sql=mysql_query("SELECT* FROM admin WHERE id='$id' AND pass='$pwd'");
if($sql)
{
 die ("Login");
}
else
{
  die ("Failed");
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset="UTF-8" /> 
  <title>
    HTML Document Structure
  </title>
  <!--<link rel="stylesheet" type="text/css" href="style.css" />!-->
</head>
<body>

  <form method="POST">
    <h1>Welcome</h1>
    <div class="inset">
      <p>
        <label for="email">Login</label>
        <input type="text" name="email" id="email">
      </p>
      <p>
        <label for="password">PASSWORD</label>
        <input type="password" name="password" id="password">
      </p>
    </div>
    <p class="p-container">
      <span>Forgot password ?</span>
      <input type="submit" name="Login" id="Login" value="Log in">
    </p>
  </form>

</body>
</html>

代码直接执行if-else语句而不等待输入

<?php
if(mysql_connect("localhost","root","")==false)
{
  die ("Connection Failed");
}
mysql_select_db("fb");
$id=$_POST["email"];
$pwd=$_POST["password"];
$sql=mysql_query("SELECT* FROM admin WHERE id='$id' AND pass='$pwd'");
if($sql)
{
 die ("Login");
}
else
{
  die ("Failed");
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset="UTF-8" /> 
  <title>
    HTML Document Structure
  </title>
  <!--<link rel="stylesheet" type="text/css" href="style.css" />!-->
</head>
<body>

  <form method="POST">
    <h1>Welcome</h1>
    <div class="inset">
      <p>
        <label for="email">Login</label>
        <input type="text" name="email" id="email">
      </p>
      <p>
        <label for="password">PASSWORD</label>
        <input type="password" name="password" id="password">
      </p>
    </div>
    <p class="p-container">
      <span>Forgot password ?</span>
      <input type="submit" name="Login" id="Login" value="Log in">
    </p>
  </form>

</body>
</html>
然后告诉它在输入后执行这些操作

if($_POST) {
    $id=$_POST["email"];
    $pwd=$_POST["password"];
    $sql=mysql_query("SELECT* FROM admin WHERE id='$id' AND pass='$pwd'");
    if($sql)
    {
     die ("Login");
    }
    else
    {
      die ("Failed");
    }
}
我知道这段代码容易受到SQL注入的攻击,但谁在乎这是否是一个家庭作业呢

永远不要因为项目的性质而放弃安全性。你会掉进一个陷阱,然后它会在你以后的生活中咬你。确保您的应用程序安全,不受项目的影响

代码直接执行if-else语句而不等待输入

<?php
if(mysql_connect("localhost","root","")==false)
{
  die ("Connection Failed");
}
mysql_select_db("fb");
$id=$_POST["email"];
$pwd=$_POST["password"];
$sql=mysql_query("SELECT* FROM admin WHERE id='$id' AND pass='$pwd'");
if($sql)
{
 die ("Login");
}
else
{
  die ("Failed");
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset="UTF-8" /> 
  <title>
    HTML Document Structure
  </title>
  <!--<link rel="stylesheet" type="text/css" href="style.css" />!-->
</head>
<body>

  <form method="POST">
    <h1>Welcome</h1>
    <div class="inset">
      <p>
        <label for="email">Login</label>
        <input type="text" name="email" id="email">
      </p>
      <p>
        <label for="password">PASSWORD</label>
        <input type="password" name="password" id="password">
      </p>
    </div>
    <p class="p-container">
      <span>Forgot password ?</span>
      <input type="submit" name="Login" id="Login" value="Log in">
    </p>
  </form>

</body>
</html>
原因是您将整个代码(HTML/PHP/SQL)放在一个文件中,没有条件语句来控制它

将submit按钮的name元素与
if(isset($\u POST['Login'])
一起使用可以解决这个问题

另一种选择是使用两个独立的文件。一个用于表单,另一个用于PHP/SQL,并为表单的操作设置
action=“handler.PHP”

相当于
(self)


但是谁在乎呢
每个不想给出或成为坏榜样的人都会这样做。非常感谢你,这是给我的建议。:)