首页 > 解决方案 > 如何解决:值未分配给 PHP 中的变量

问题描述

HTML 表单有一个输入字段。用户将在 HTML 表单中输入值,并根据该值采取进一步的步骤。

输入字段接受字符,这就是正在输入的内容。在 HTML 表单中,它被正确定义。

问题是输入的值没有存储在 PHP 的变量中。这真的很简单,但由于某种原因,我无法发现问题。

请注意,这不是将要部署的实际代码。我正在处理已部署代码中的 SQL 注入。只需要弄清楚这里出了什么问题。

我尝试显示表单中输入的值。它只返回 NULL。我在这里做错了什么?

<?php
$host = 'localhost';
$dbname = 'dbname';
$username = 'user';
$password = 'password';


$mysqli = new mysqli("localhost", "user", "password", "dbname");

if($mysqli->connect_error)
{
die("$mysqli->connect_errno: $mysqli->connect_error");
}
$field1=$_POST['field1'];

var_dump($field1);
?>

HTML 代码:

<!DOCTYPE html>
<html> 
<body>
<basefont face = "sans-serif" size = "2" color = "#ff0000">
<form id="queryForm" method="POST" action="/test.php">
<div class="row">
<fieldset class="hm-fieldset">
<legend class="hm-legend">Enter Query Criteria:</legend>
<div class="col">
<span style="font-family:sans-serif">
Field1<br> <input type="text" id="field1" style="height:10px; 
width:105px"> </span>
</div>
<button>Submit</button>
</fieldset>
</div>
</form>
</body>
</html>

标签: phphtml

解决方案


您需要为您的输入提供一个name属性,以便它们在 PHP 中工作(请参阅手册)。尝试将其更改为:

<input type="text" id="field1" name="field1" style="height:10px; width:105px">

另请参阅有关和之间区别的出色问答idname


推荐阅读