PHP脚本更新Web表单失败


PHP script failing on Updating Web Form

感谢所有帮助过我的人。我有我的脚本工作,所以我可以添加&显示条目。现在我跟随本教程更新条目,但当我运行它时,我在页面上得到这个错误:! ) 注意:未定义的索引:id在/var/www/inventory/update.php的第21行调用堆栈

时间记忆功能位置

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>
    ?/

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

时间记忆功能位置

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

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

时间记忆功能位置

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

(!)注意:未定义变量:ud_equipmnt_borrow在/var/www/inventory/update .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>

?>

第一个:你必须添加像'?id=NUMBER'在url的末尾(例如:update.php?id=1')使用id

必须使用:

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

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

$ud_last = $_POST['ud_last'];

那么它应该可以工作