【问题标题】:PHP script failing on Updating Web FormPHP 脚本在更新 Web 表单时失败
【发布时间】:2014-08-16 04:23:57
【问题描述】:

感谢所有帮助我解决之前麻烦的人。我有我的脚本工作,所以我可以添加和显示条目。现在我正在按照本教程更新条目,但是当我运行它时,我在页面上收到了这个错误: ! ) 注意:未定义索引:第 21 行 /var/www/inventory/update.php 中的 id 调用栈

时间记忆功能定位

1 0.0000 229608 {main}( ) ../update.php:0 代码如下所示:

 <?php
    ini_set("display_errors","on");

    $dsn='mysql:host=localhost;dbname=inventory_form';
    $username="***";
    $password="****";
    $database="inventory_form";


    try
        {
        $link=new PDO($dsn, $username,$password);
        echo 'Connected to MySQL Server';
        }
    catch (PDOException $e)
        {
        $error_message=$e->getMessage();
        echo "<h1>An error occurred: $error_message</h1>";
        }

    $id=$_POST['id'];
    $query="SELECT * FROM Inventory WHERE id='$id'";
    $result=$link->query($query);

    while ($row = $result->fetch()) { 
        echo '<b><i><center><font size=5>'. $row['FName'] .'</font></b></i></center><br /n>';
        echo '<b><i><center><font size=5>'. $row['LName'] .'</font></b></i></center><br /n>';
        echo '<b><i><center><font size=5>'. $row['Eqpmnt_Brwd'] .'</font></b></i></center><br /n>';
        echo '<b><i><center><font size=5>'. $row['Service_Tag'] .'</font></b></i></center>';
        echo '<b><i><center><font size=5>'. $row['Date_Taken'] .'</font></b></i></center>';
        echo '<b><i><font size=5>'. $row['Comments'] .'</font></b></i>';

      }

    ?>

    <form action="updated.php" method="post">
    <input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
    <font size=5>First Name:</font><input type="text" name="ud_first" maxlength="12" 
size="12" />
    <font size=5>Last Name:</font><input type="text" name="ud_last" maxlength="36" size="12" />
    <font size=5>Equipment Borrowed:</font><input type="text" name="ud_Equipment_Borrowed" maxlength="60" size="14" /><br />
    <font size=5>Service Tag:</font><input type="text" name="ud_Service_Tag" maxlength="6" size="6" /><br />
    <font size=5>Date Taken:</font><input type="text" name="ud_Date_Taken" /><br />
    <font size=5>Additional Comments:</font><br /> 
        <textarea name="ud_Comments" maxlength="150" cols="50" rows="3"></textarea> <br /> <br />
    <input type="Submit" value="Update">
    </form>
    ?/

然后我从 updated.php 脚本中得到一堆错误: 注意:未定义的变量:第 19 行 /var/www/inventory/updated.php 中的 ud_first 调用栈

时间记忆功能定位

1 0.0002 231664 {main}( ) ../updated.php:0

( ! ) 注意:未定义变量:第 19 行 /var/www/inventory/updated.php 中的 ud_last 调用栈

时间记忆功能定位

1 0.0002 231664 {main}( ) ../updated.php:0

( ! ) 注意:未定义变量:ud_Equipmnt_borrowed in /var/www/inventory/updated.php 第 19 行 等所有变量。 该脚本是:

<?php
ini_set("display_errors","on");
$dsn='mysql:host=localhost;dbname=inventory_form';
$username="***";
$password="****";
$database="inventory_form";

try
    {
    $link=new PDO($dsn, $username,$password);
    echo 'Record added succesfully';
    }
catch (PDOException $e)
    {
    $error_message=$e->getMessage();
    echo "<h1>An error occurred: $error_message</h1>";
    }

$query="UPDATE contacts SET FName='$ud_first', LName='$ud_last', Eqmnt_Brwd='$ud_Equipmnt_borrowed', Service_Tag='$ud_Service_Tag', Date_Taken='$ud_Date_Taken', Comments='$ud_Comments' WHERE id='$ud_id'";

echo "Record Updated";

?>

非常感谢任何帮助。我对如何使用 id 字段来使这一切正常工作有点困惑。根据教程,我应该准备好填写表格以编辑特定条目。提前致谢

更新。这是代码:

<?php
ini_set("display_errors","on");

$dsn='mysql:host=localhost;dbname=inventory_form';
$username="***";
$password="*****";
$database="inventory_form";


try
    {
    $link=new PDO($dsn, $username,$password);
    echo 'Connected to MySQL Server';
    }
catch (PDOException $e)
    {
    $error_message=$e->getMessage();
    echo "<h1>An error occurred: $error_message</h1>";
    }

$id=htmlspecialchars($_GET['id']);

$query="SELECT * FROM Inventory WHERE id='$id'";
$result=$link->query($query);

while ($row = $result->fetch()) { 
    echo '<b><i><center><font size=5>'. $row['FName'] .'</font></b></i></center><br /n>';
    echo '<b><i><center><font size=5>'. $row['LName'] .'</font></b></i></center><br /n>';
    echo '<b><i><center><font size=5>'. $row['Eqpmnt_Brwd'] .'</font></b></i></center><br /n>';
    echo '<b><i><center><font size=5>'. $row['Service_Tag'] .'</font></b></i></center>';
    echo '<b><i><center><font size=5>'. $row['Date_Taken'] .'</font></b></i></center>';
    echo '<b><i><font size=5>'. $row['Comments'] .'</font></b></i>';

  }

?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
<font size=5>First Name:</font><input type="text" name="ud_first" maxlength="12" size="12" />
<font size=5>Last Name:</font><input type="text" name="ud_last" maxlength="36" size="12" />
<font size=5>Equipment Borrowed:</font><input type="text" name="ud_Equipment_Borrowed" maxlength="60" size="14" /><br />
<font size=5>Service Tag:</font><input type="text" name="ud_Service_Tag" maxlength="6" size="6" /><br />
<font size=5>Date Taken:</font><input type="text" name="ud_Date_Taken" /><br />
<font size=5>Additional Comments:</font><br /> 
    <textarea name="ud_Comments" maxlength="150" cols="50" rows="3"></textarea> <br /> <br />
<input type="Submit" value="Update">
</form>


?>

【问题讨论】:

  • 您的查询中的变量是否已初始化?
  • 看!您正在从库存表中获取数据并更新联系人表。你在做什么?
  • 你是对的!是我正在查看的代码遗留下来的。将其更改为更正 db,但如果您在下面查看 pastebin 站点脚本仍然显示注意:未定义索引:第 21 行 /var/www/inventory/update.php 中的 id 和注意:未定义变量:/var/www/inventory 中的 ud_id /updated.php 在第 26 行,用于调用的 updated.php 脚本

标签: php


【解决方案1】:

第一个:您必须在 url 末尾添加类似 '?id=NUMBER' 的内容(例如:update.php?id=1')才能使用 ID

为此,您必须使用:

$id = htmlspecialchars($_GET['id'])

第二个:您使用了 $ud_last 等,但没有在代码中定义它们,您必须这样做:

$ud_last = $_POST['ud_last'];

那么它应该可以工作

【讨论】:

  • 对不起,我加了一行: $id = htmlspecialchars($_GET['id']) $id=$_POST['id']; $query="SELECT * FROM Inventory WHERE id='$id'"; $result=$link->query($query);我只是得到白页。我什至没有收到连接消息。
  • 您现在已经定义了两次 $id,在这种情况下您不应该使用 $_POST['id'] 定义 $id(您必须使用它来定义那些 ud_last 等)跨度>
  • 这是否仍然得到白页:
  • 奇怪,它至少应该给出错误;S 你能把整个更新的代码放在你的帖子或@jsfiddle.net 中吗?
  • 收到错误信息:! ) 注意: Undefined index: id in /var/www/inventory/update.php on line 21 Call Stack # Time Memory Function Location 1 0.0004 232088 {main}( ) ../update.php:0 code is :$id= htmlspecialchars($_GET['id']); $query="SELECT * FROM Inventory WHERE id='$id'"; $result=$link->query($query);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多