更新Web表单时PHP脚本失败

更新Web表单时PHP脚本失败,php,Php,感谢所有在以前的麻烦中给我帮助的人。我有我的脚本工作,所以我可以添加和显示条目。现在,我跟随本教程更新条目,但当我运行它时,我在页面上遇到了以下错误: ! 注意:第21行/var/www/inventory/update.php中的未定义索引:id 调用堆栈 时间记忆功能定位 1 0.0000 229608{main}../update.php:0 代码如下所示: <?php ini_set("display_errors","on"); $dsn='mysql:ho

感谢所有在以前的麻烦中给我帮助的人。我有我的脚本工作,所以我可以添加和显示条目。现在,我跟随本教程更新条目,但当我运行它时,我在页面上遇到了以下错误: ! 注意:第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行的未定义变量:ud_first in/var/www/inventory/updated.php 调用堆栈

时间记忆功能定位 1 0.0002 231664{main}../updated.php:0

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

时间记忆功能定位 1 0.0002 231664{main}../updated.php:0

!!注意:未定义变量:ud_Equipmnt_借用/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>


?>

第一个:您必须在url示例update.php?id=1的末尾添加类似于“?id=NUMBER”的内容来使用id

为此,您必须使用:

$id = htmlspecialchars($_GET['id'])
第二个:您使用了$ud_last等,但没有在代码中定义它们,您必须这样做:

$ud_last = $_POST['ud_last'];

那么它应该可以工作了

查询中的变量是否已初始化?看!您正在从库存表获取数据并更新联系人表。你在干什么?你说得对!是我在看代码时留下的。将其更改为更正db,但如果您查看下面的pastebin站点脚本,则第21行的/var/www/inventory/update.php中仍然显示Notice:Undefined index:id,第26行的/var/www/inventory/updated.php中仍然显示updated.php脚本:$id=htmlspecialchars$\u GET['id']id=$\u POST['id']; $查询=从库存中选择*,其中id=“$id”$结果=$link->query$query;我只是得到了一张白纸。我甚至没有收到连接消息。你已经定义了$id两次了,你不应该使用$\u POST['id']来定义$id。在这种情况下,你必须定义那些ud\u last等。使用它仍然会得到白色页面:奇怪,它至少应该给出错误;你能把全部更新的代码放在你的帖子或@jsfiddle.net中吗?获取错误消息:!注意:第21行/var/www/inventory/update.php中未定义的索引:id调用堆栈时间内存函数位置1 0.0004 232088{main}../update.php:0代码是:$id=htmlspecialchars$_GET['id']$查询=从库存中选择*,其中id=“$id”$结果=$link->query$query;