gpt4 book ai didi

PHP - 来自 HTML 表单的未定义索引 - 显示为 NULL

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:38 25 4
gpt4 key购买 nike

所以我写了一个程序,从 MySQL 数据库生成一个简单的报告。然而,在更改代码以接受来自 HTML 表单的用户输入后,我突然收到各种错误,所有错误都源于 undefined index (变量显示为空)。

如果我不通过表单接受用户输入,该程序将完美运行。

到目前为止我已经尝试过...

  1. 从 POST 切换到 GET。
  2. 使用 if(isset 语句。

不多,我知道,但我对 PHP 的了解并不止于此。

具体错误如下:

“ undefined variable :第 35 行中的 B_DATE”和“第 36 行中...中的 E_DATE”

还有很多,但都源于上述问题。

在发布代码之前,我在代码的开头放置了一个调试数组,它输出以下内容:

 $debug = true;
if ($debug === true)
{
echo '<hr />
<h4>Debug Area</h4>
<pre>';

print_r(array($_GET, $_POST));

echo '</pre>
<hr />';
}

Debug Area

Array
(
[0] => Array
(
[formID] => IRCcalculatepercentageform
[B_DATE] => 2014-10-12
[E_DATE] => 2014-10-13
)

[1] => Array
(
)

)

 if(!isset($_GET['IRCcalculatepercentageform'])){
echo "No value";
}

返回“无值”。

这是 HTML 表单的代码:

 <form method="get" action="IRCcalculatepercentage1.php"> 

<input type="hidden" name="formID" value="IRCcalculatepercentageform" />

<p> Type in the date you want to perform your calculation on (year, month, day): "0000-00-00" </p>
<p> Beginning Date: <input type="text" name="B_DATE" /></p>
<p> Ending Date: <input type="text" name="E_DATE" /></p>

<input type="submit" value="Submit" />
</form>
</body>
</html>

程序代码如下:

<?php //calculate percentage

error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once 'IRCconfig.php';

$connection =
new mysqli($db_hostname, $db_username, $db_password, $db_database);

if ($connection->connect_error) die($connection->connect_error);

$B_DATE = $_GET['B_DATE'];
$E_DATE = $_GET['E_DATE'];

/* Computes the number of cells from column
'C_LAB' that contains the term 'Y'*/

$query1 = "SELECT C_LAB, DATE
FROM CLIENT_CHECKIN
WHERE DATE BETWEEN '$B_DATE' AND '$E_DATE'
HAVING C_LAB = 'Y'";


$result1 = $connection->query($query1);
if (!$result1) die($connection->error);

$rows1 = $result1->num_rows;

for ($j = 0 ; $j < $rows1 ; ++$j)
{
$result1->data_seek($j);
echo 'Computer lab: ' . $result1->fetch_assoc()['C_LAB'] . '<br><br>';
$count1 = $j;
}

/*Computes the number of cells from column 'C_LAB'
that contain the term 'N'*/

$query2 = "SELECT C_LAB, DATE
FROM CLIENT_CHECKIN
WHERE DATE BETWEEN '$B_DATE' AND '$E_DATE'
HAVING C_LAB = 'N'";

$result2 = $connection->query($query2);
if (!$result2) die($connection->error);

$rows2 = $result2->num_rows;

for ($l = 0 ; $l < $rows2 ; ++$l)
{
$result2->data_seek($l);
echo 'Computer lab: ' . $result2->fetch_assoc()['C_LAB'] . '<br><br>';
$count2 = $l;
}

$total = ($count1 + 1) + ($count2 + 1);

echo "The number of clients who used the computer lab is ", $count1 + 1, "<br><br>",
"The total number of clients who did not use the computer lab is ", $count2 + 1, "<br><br>",
"The total number of clients today is ", $total, "<br><br>";

echo "The percentage of clients who used the computer lab today is ", (($count1 + 1) / $total) * 100,
"%";

$result1->close();
$connection->close();

?>

最佳答案

根据您的 originally posted question :

您正在使用 name="B_Date"name="E_Date" 来配合 $_GET['B_DATE']$_GET['E_DATE'] 这是您的代码失败的主要原因。这些变量区分大小写。

需要修改为:

name="B_DATE"name="E_DATE"

这就是为什么你的变量得到了一个 Undefined index...

<p> Beginning Date: <input type="text" name="B_Date" /></p>
<p> Ending Date: <input type="text" name="E_Date" /></p>

需要修改为:

<p> Beginning Date: <input type="text" name="B_DATE" /></p>
<p> Ending Date: <input type="text" name="E_DATE" /></p>

然后为它们使用isset():

if(isset($_GET["B_DATE"]) && isset($_GET["E_DATE"])) {

$B_DATE = $_GET['B_DATE'];
$E_DATE = $_GET['E_DATE'];

// ...

$query2 = "SELECT C_LAB, DATE
FROM CLIENT_CHECKIN
WHERE DATE BETWEEN '$B_DATE' AND '$E_DATE'
HAVING C_LAB = 'N'";

// rest of your code down to...

$result1->close();
$connection->close();

}

但是,使用这种方法会使您打开 SQL injection .使用 prepared statements , 或 PDO with prepared statements .


您也可以使用 mysqli_real_escape_string() 作为保护措施

$B_DATE = mysqli_real_escape_string($connection,$_GET['B_DATE']);
$E_DATE = mysqli_real_escape_string($connection,$_GET['E_DATE']);

和:

$query2 = "SELECT C_LAB, DATE 
FROM CLIENT_CHECKIN
WHERE DATE BETWEEN '".$B_DATE."' AND '".$E_DATE."'
HAVING C_LAB = 'N'";

另一种使用 stripslashes() 的方法和 mysqli_real_escape_string() :

$B_DATE = stripslashes($_GET['B_DATE']);
$B_DATE = mysqli_real_escape_string($connection,$_GET['B_DATE']);

$E_DATE = stripslashes($_GET['E_DATE']);
$E_DATE = mysqli_real_escape_string($connection,$_GET['E_DATE']);

关于PHP - 来自 HTML 表单的未定义索引 - 显示为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26329150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com