新手尝试php:未定义索引



我正试图使用url将数据从应用程序发送到php脚本,然后php脚本将url参数中的数据插入到mysql数据库中。下面是我的(基本的和完全剥离的)代码,我知道我错过了错误处理等事情。

<?php
$hoppie_test=$_GET['{hoppie_test}'];
$servername= "localhost";
$username= "root";
$password= "";
$db="messageserver";
$conn = new mysqli($servername, $username, $password, $db);
$sql = "insert into test(hoppie.test, time)values('$hoppie_test')";
$conn->close();
?> 

我用来发送数据的应用程序,使用带有参数的url来传输消息/数据:

<url>http://localhost/acars.php?${hoppie_test}</url>

我不断收到以下错误:

Notice: Undefined index: {hoppie_test} in D:XAMPPhtdocsacars.php on line 3

我在这里有点疯了,从研究中尝试了很多可能的选择。我想我对这一切太陌生了,但我正在努力学习。任何建议或提示都非常受欢迎
谢谢

简单地说,您的"未定义索引"通知将是您试图访问不存在的数组元素。

在这种情况下(如果没有在<url>...</url>字符串中看到${hoppie_test}的输出,我无法告诉你数组键是什么,但在PHP中,格式如下:<url>yoururl.com?key=value&key=value...</url>。你可以通过简单地运行print_r($_GET)并发现你的参数正在按预期传递来查看这个结构。

尽管正如Dharman在评论中指出的那样,您所采用的方法非常脆弱,并且存在不良做法(因为您让用户输入直接与数据库交互)。

最新更新