将值从表单插入数组,然后插入数据库

时间:2012-12-08 08:39:12

标签: php mysql arrays forms

我想从HTML表单中获取用户值到数组,然后将其插入到mysql数据库中。它将正确的值插入数据库,然后它会发出以下错误:

  

注意:未定义的索引:第153行的C:\ wamp \ www \ Capturing System \ All_Topics_widget.php中的TopicNum

以下内容位于页面顶部:

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

&GT?;

$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!';
                    }


            }
    }

&GT?; 以下代码位于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>

2 个答案:

答案 0 :(得分: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)

as

            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

答案 1 :(得分:1)

Notice正在显示,因为您在第153行引用了$_POST['TopicNum'],此时此$_POST[]变量已不再设置。

尝试在if(empty($_POST) == false && empty($errors)== true) {}

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

这就是导致Notice -

的原因

1 =&GT;您提交/ POST表单,从而设置$_POST['TopicNum']并在第153行使用。
2 =&gt;您可以在第172行capture_topic($capture_topic);上将值输入数据库 3 =&gt;您重定向到174 header('Location: All_Topics_widget.php?success');上的页面,从而清除$_POST[]全局变量,因此当在153再次调用它时,它现在是未定义的。通过在if(empty($_POST) == false && empty($errors)== true)的支票中移动它,只有在表格提交/张贴时才会被引用