-2

我是一名 PHP 程序员,我对下面的问题感到困惑,我正在等待您的指导。非常感谢!有html代码

 <form action="" method="POST">
    <div>
        <strong>Release: *</strong> <input type="text" name="Release" value="<?php echo $rel; ?>" /><br/>
        <strong>User Story ID: *</strong> <input type="text" name="User Story ID" value="<?php echo $id; ?>" /><br/>
        <strong>Test Owner *</strong> <input type="text" name="Test Owner" value="<?php echo $owner; ?>" /><br/>
        <strong>Date of TC Review *</strong> <input type="text" name="Date of TC Review" value="<?php echo $data; ?>" /><br/>
        <strong>By Design </strong> <input type="text" name="By Design" value="<?php echo $design; ?>" /><br/> 
        <strong>By Review </strong> <input type="text" name="By Review" value="<?php echo $review; ?>" /><br/> 
        <strong>By Defect </strong> <input type="text" name="By Defect" value="<?php echo $defect; ?>" /><br/> 
      <p>* required</p>
      <input type="submit" name="submit" value="Submit">
    </div>
 </form> 

有php代码

     if (isset($_POST['submit']))
     { 
        // get form data, making sure it is valid
         $release = mysql_real_escape_string(htmlspecialchars($_POST['Release']));
        //  $ID = " abc";
        echo $_POST['Release'];
        echo $_POST['User Story ID'];
        $ID = mysql_real_escape_string(htmlspecialchars($_POST["User Story ID"]));
        $T_Owner = mysql_real_escape_string(htmlspecialchars($_POST['Test Owner']));
        $data = mysql_real_escape_string(htmlspecialchars($_POST['Date of TC Review']));
        $T_ByDesign= mysql_real_escape_string(htmlspecialchars($_POST['By Design']));
        $T_ByReview= mysql_real_escape_string(htmlspecialchars($_POST['By Review']));
        $T_ByDefect= mysql_real_escape_string(htmlspecialchars($_POST['By Defect']));

        // check to make sure both fields are entered
        if ($release == '' || $ID == ''||$T_Owner==''||$data=='')
        {
            // generate error message
            $error = 'ERROR: Please fill in all required fields!';

            // if either field is blank, display the form again
            renderForm($release, $ID, $T_Owner, $data, $T_ByDesign, $T_ByReview, $T_ByDefect, $error);
        }
        else
        {
            // save the data to the database
            mysql_query("INSERT Tests SET T_Release='$release', ID='$ID',TestOwner='$T_Owner',T_Date='$data',Test_ByDesign='$T_ByDesign',Test_ByReview='$T_ByReview',Test_ByDefect='$T_ByDefect'")
            or die(mysql_error()); 

            // once saved, redirect back to the view page
            header("Location: view.php"); 
        }
 }

错误信息如下:

Notice: Undefined index: User Story ID in C:\xampp\htdocs\Test\new.php on line 47
abc
Notice: Undefined index: User Story ID in C:\xampp\htdocs\Test\new.php on line 55

Notice: Undefined index: User Story ID in C:\xampp\htdocs\Test\new.php on line 56

Notice: Undefined index: Test Owner in C:\xampp\htdocs\Test\new.php on line 57

Notice: Undefined index: Date of TC Review in C:\xampp\htdocs\Test\new.php on line 58

Notice: Undefined index: By Design in C:\xampp\htdocs\Test\new.php on line 59

Notice: Undefined index: By Review in C:\xampp\htdocs\Test\new.php on line 60

Notice: Undefined index: By Defect in C:\xampp\htdocs\Test\new.php on line 61
4

6 回答 6

2

第一次编辑您的表格:

<form action="" method="POST">
<div>
    <strong>Release: *</strong> <input type="text" name="Release" value="<?php echo $rel; ?>" /><br/>
    <strong>User Story ID: *</strong> <input type="text" name="User_Story_ID" value="<?php echo $id; ?>" /><br/>
    <strong>Test Owner *</strong> <input type="text" name="Test_Owner" value="<?php echo $owner; ?>" /><br/>
    <strong>Date of TC Review *</strong> <input type="text" name="Date_of_TC_Review" value="<?php echo $data; ?>" /><br/>
    <strong>By Design </strong> <input type="text" name="By_Design" value="<?php echo $design; ?>" /><br/> 
    <strong>By Review </strong> <input type="text" name="By_Review" value="<?php echo $review; ?>" /><br/> 
    <strong>By Defect </strong> <input type="text" name="By_Defect" value="<?php echo $defect; ?>" /><br/> 
  <p>* required</p>
  <input type="submit" name="submit" value="Submit">
</div>

不要在表单名称上使用空格,那么您可以获得这样的帖子值:

if (isset($_POST['submit']))
 { 
       // get form data, making sure it is valid
      $release = mysql_real_escape_string(htmlspecialchars($_POST['Release']));

    echo Print_r($_POST['Release']);
    echo Print_r($_POST['User_Story_ID']);
    $ID = $_POST["User_Story_ID"];
    $T_Owner = $_POST['Test_Owner'];
    $data = $_POST['Date_of_TC_Review'];
    $T_ByDesign= $_POST['By_Design'];
    $T_ByReview= $_POST['By_Review'];
    $T_ByDefect= $_POST['By_Defect'];

    // check to make sure both fields are entered
if ($release == '' || $ID == ''||$T_Owner==''||$data=='')
{
    // generate error message
    $error = 'ERROR: Please fill in all required fields!';

    // if either field is blank, display the form again
    renderForm($release, $ID, $T_Owner, $data, $T_ByDesign, $T_ByReview, $T_ByDefect, $error);
}
else
{
    // save the data to the database
    mysql_query("INSERT Tests SET T_Release='$release', ID='$ID',TestOwner='$T_Owner',T_Date='$data',Test_ByDesign='$T_ByDesign',Test_ByReview='$T_ByReview',Test_ByDefect='$T_ByDefect'")
    or die(mysql_error()); 

    // once saved, redirect back to the view page
    header("Location: view.php"); 
}
于 2013-01-24T06:29:00.533 回答
2

您为表单输入元素使用了无效名称。不要在表单名称中包含空格。

于 2013-01-24T06:20:26.783 回答
1

首先,永远不要在 php 或任何其他编程中使用“By Design”类型的编码,而不是使用它,您应该像这样使用“By_Design”或使用 cameCaps(byDesign)。什么是你的html表单中的$rel,$id和其他东西,你没有提到它。导致您的问题的主要问题是在表单和 php 中使用空格。请不要使用它。

于 2013-01-24T06:25:51.593 回答
1

我尝试了一些测试代码:

<form action="" method="POST">
<input type="submit" name="what happened" value="here" />
</form>
<?php
print_r($_POST);
?>

似乎 PHP 将所有元素名称中的空格替换为下划线。因此,您想使用$_POST['User_Story_ID']而不是$_POST['User Story ID']. 等等。

于 2013-01-24T06:27:17.223 回答
0

您应该在使用前定义变量。请再检查一次。

于 2013-01-24T06:38:11.113 回答
0

添加表单操作“ your_page.php

例子 : <form action="your_page.php" method="POST">

并删除表单输入元素的空间,例如:

echo $_POST['User Story ID'];

而不是这个:

例子 :

<strong>User Story ID: *</strong> <input type="text" name="UserStoryID" value="<?php echo $id; ?>" /><br/>

echo $_POST['UserStoryID'];或任何你想命名的东西

于 2013-01-24T06:22:54.310 回答