1

ユーザー値を HTML フォームから配列に取得し、それを mysql データベースに挿入したいと考えています。データベースに正しい値を挿入すると、次のエラーが発生します。

注意: 未定義のインデックス: C:\wamp\www\Capturing System\All_Topics_widget.php の 153 行目の TopicNum

以下は、ページの上部にあります。

<?php 
include 'scripts/functions/init.php';
Restrict();

?>

$userid = $_SESSION['userid'];
$KM_Number = $_SESSION['C_KM'];
$query = "SELECT * FROM knowledge_modules where km_number = "."'".$KM_Number."'";
$Knowledge = mysql_query($query) or die(mysql_error);
$row = mysql_fetch_assoc($Knowledge);
$Module_type = 'Knowledge Modules';
$Purpose_M = $_SESSION['Purpose_KM'];
$KM = 'KM';
$NUM = $_SESSION['KM'];
$KT = 'KT';

if (empty($_POST)=== false)
    {
        $R_fields = array('Topic_Num','Title','Weight');
        foreach($_POST as $key=>$value)
        {
            if (empty($value) && in_array($key,$R_fields)=== true)
                {
                    $errors[] = 'fields marked with (*) are required';
                    break 1;
                }
        }

        if(empty($errors)=== true)
            {
                if(topic_exists($_POST['TopicNum']))
                    {
                        $errors[] = 'Sorry, the Topic already exist!';
                    }


            }
    }

?> 次のコードは HTML タグ内にあります。

<h2>Tools - </h2>

            <form action="All_Topics_widget.php" method="POST" enctype="multipart/form-data">
              <fieldset>
                <table border="0">
                    <tr>
                        <td><label for="TopicNum">Topic Number:*&nbsp<?php echo $KM.'-'.$NUM.'-'.$KT ?></label></td>
                        <td><input type="text" size="5" name="TopicNum" /></td>
                    </tr>
                    <tr>
                        <td><label for="Title">Title:*</label></td>
                        <td><input type="Text" size="35" name="Title"/></td>
                    </tr>
                    <tr>
                        <td><label for="Weight">Weight:*</label></td>
                        <td><input type="text" size="05" name="Weight" /></td>

                    </tr>

                </table>
            </fieldset>
              <fieldset>
                       <table border="0">
                            <tr>
                                <td><input id="Submit" type="submit" value="Capture" /></td>
                                <td><input id="Reset1" type="reset" value="reset" /></td>
                                 <td><a href="list_topics.php">Capture Topics</a></td>

                           </tr>
                        </table>
              </fieldset>
              <?php

                    $Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];//This is line 153
                    if(isset($_GET['success']) && empty($_GET['success']))
                        {
                            echo 'Succefully Captured';
                        }
                    else
                        {
                            if(empty($_POST) == false && empty($errors)== true)
                                {

                                    //Capture data from the fields to an array
                                    $capture_topic = array(
                                    'TopicNum' => $Topics,
                                    'Title'=>$_POST['Title'],
                                    'Weight'=>$_POST['Weight'],
                                    'Purpose_M'=>$Purpose_M,
                                    'userid'=>$userid);                                     

                                    //Submit the data into the database
                                    capture_topic($capture_topic);
                                    //redirect to the Modules widget page till the user press NEXT Page
                                    header('Location: All_Topics_widget.php?success');
                                    exit();

                                }

                            else if(empty($errors)== false)
                                {
                                    //Display errors

                                    echo output($errors);
                                }
                        }

              ?>
       </form>
4

2 に答える 2

1

これは、この変数が設定されていない153 行Noticeを参照しているためです 。$_POST['TopicNum']$_POST[]

153行目を内側に移動してみてください if(empty($_POST) == false && empty($errors)== true) {}

 if(empty($_POST) == false && empty($errors)== true)
        {
        $Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];//This is line 153
         ...
         }

これが原因となっている理由ですNotice-

1=> フォームを送信/POST し、$_POST['TopicNum']153 行で設定して使用します。
2=> 172 行で値をデータベースに入力しますcapture_topic($capture_topic);
3=> 174 のページにリダイレクトし、グローバルheader('Location: All_Topics_widget.php?success');をクリアします。 $_POST[]153 で再度呼び出され、現在は未定義です。チェック内に移動するとif(empty($_POST) == false && empty($errors)== true)、フォームが送信/投稿されたときにのみ参照されます

于 2012-12-08T09:14:13.517 に答える
1

これを見て

                            header('Location: All_Topics_widget.php?success');
                            exit();

POST データを正常に処理したら、スクリプトをそれ自体にリダイレクトします。このリダイレクトの後、POST データは存在しません。

リライト

          $Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];//This is line 153
            if(isset($_GET['success']) && empty($_GET['success']))
                {
                    echo 'Succefully Captured';
                }
            else
                {
                    if(empty($_POST) == false && empty($errors)== true)

なので

            if(isset($_GET['success']) && empty($_GET['success']))
                {
                    echo 'Succefully Captured';
                }
            else
                {
                    $Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];
                    if(empty($_POST) == false && empty($errors)== true)

通知を抑制します。

必須フィールドの配列に注意してください

$R_fields = array('Topic_Num','Title','Weight');

Topic_Num という名前のフィールドが表示されることを期待していますが、フォームの名前は TopicNum です。

于 2012-12-08T08:48:39.660 に答える